TIPS & TRICKS
To search the contents of the man pages
# man -k searchterm |
Script bash tasks examples
Script bash 1
| Create a script file:
| - if the paramater is “GM” it outputs “good morning”
| - if it is “GN”, it outputs “good night”
|
# chmod 755 script.sh |
Script bash 2
| Question: Take a input as a file and then print its contents line-by-line
| - if no input give then print: Usage: ./readfile.sh
| - it should take only one argument
|
Script bash 3
| Question: Script should take only one input as argument
| - It should print
| - YES for Y/y
| - NO for N/n
| - UNKNOWN for anything else
| - For anything else print: Usage: ./caseex.sh Y|N
|
Script bash 4
| Question: Script should take one argument only with values
| - Yes|YES it should print awesome
| - No|NO it should print That’s bad
| - For anything else it should print it’s usage as: Usage: ./ifelse.sh Yes|No
|
Script bash 5
| Create a newsearch script called
| The script is placed /usr/binunder
| This script is used to find /usrall files that are greater than 30k
, but less than 50k
and have SUIDpermissions, and place these file names in the /root/newfilesfile
|
# chmod +x /usr/bin/newsearch |
Script bash 6
| Create a myresearch script called
| The script is placed /usr/binunder
| This script is used to find /usrall
files that are smaller than 10m
and have modification Group ID permissions s
, and place these files /root/myfilesunder
|
# chmod +x /usr/bin/newsearch |
Script bash 7
| Question: Script should take a number as an argument and should print HACK that may times
| - Example forex.sh 4 will print HACK 1, HACK 2, HACK 3, HACK 4 in diffrent lines
| - If number is less than or equal to zero then it should print: number less than 0
| - If inout is not a number than it should print: Input not a number
| - For any other number of inputs it should print: Usage ./forex.sh
| - If input is NOT a number it should throw error code 6 and should print Input not a number.
|
Script bash 8
| Question: Script should take only one input numberic input
| - It should print HACK N that many times
| - If number of agruments is not 1 then it should print: Usage: ./whilex.sh
| - If input is not number then print: NOT a number
| - If number is less than equal to 0 then print: Number not > 0
| - We must use while loop to implement it
|
Script bash 9
| Write a bash shell script that creates three users:
| - user555, user666, user777
| - with nologin shell
| - passwords matching their names
| The script should also
| - extract names of these three new users from the /etc/passwd
| - redirect them to /var/tmp/newusers
|
Script bash 10
| Create a bash script that display
| - the hostname,
| - users that are currently logged in
|
Script bash 11
| Create a bash script that shows
| - total count of the supplied arguments
| - value of the first argument
| - PID of the script
| - and all the supplied arguments
|
Script bash 12
| Create a bash script that can create user10, user20 and user30 accounts with each account is create a message saying “The account is created successfuly” will be displayed otherwise the script will terminate .
| In case of a successful account creation assign the user account a password as their username
|
Script bash and cron
Script bash and cron 1
| Créez un script bash nommé backup.sh
qui archive le répertoire /data
vers /backup/data.tar.gz
chaque jour à 2h00.”
Créer le script backup.sh
:
|
Rendre le script exécutable :
# chmod +x /backup.sh |
Planifier la tâche cron :
# crontab -e |
Script bash and cron 2
| As a System Administrator you are responsible to take a backup of your /etc directory every night.
| Build a shell script to take a backup of the /etc/directory using the tar command.
| The backup script should be named as /root/backup.sh.
| Schedule this script to run at 11:00 PM every night – except Sundays.
|
# chmod +x /root/backup.sh |
# crontab -e |
Exemples de scripts shell basiques
Exemple de script shell typique avec des structures de contrôle :
|
Documentations
Internet
Git
ChatGPT