Linux shell 脚本实例代码分享,讲解关于Linux shell使用脚本的方法。
#!/bin/bash name=Tom if grep $name databasefile > /dev/null 2>&1 then : else echo $1 not found in databasefile exit 1 fi #!/bin/bash # Scriptname: nosy echo -e Are you happy? /c read answer echo $answer is the right response. echo -e What is your full name? /c read first middle last echo Hello $first echo -n Where do you work? read echo I guess $REPLY keeps you busy! #------------------------------------------------ read -p Enter your job title: echo I thought you might be an $REPLY. echo -n Who are your best friends? read -a friends echo Say hi to ${friends[2]}. #!/bin/sh # File: perm_check # Using the old style test command file=./testing file=./c8 if [ -d $file ] then echo $file is a directory elif [ -f $file ] then if [ -r $file -a -w $file -a -x $file ] then # nested if command echo You have read,write,and execute / permission on $file. fi else echo $file is neither a file nor a directory. fi #!/bin/sh # File: perm_check2 # Using the old style test command file=./testing if [[ -d $file ]] then echo $file is a directory elif [[ -f $file ]] then if [[ -rwx $file ]] then # nested if command echo You have read,and execute / permission on $file. fi else echo $file is neither a file nor a directory. fi #!/bin/bash # Scriptname: printer_check # Script to clear a hung up printer if [ $LOGNAME != root ] then echo Must have root privileges to run this program exit 1 fi cat << EOF Warning: All jobs in the printer queue will be removed. Please turn off the printer now. Press return when you are ready to continue. Otherwise press Control C. EOF read JUNK # Wait until the user turns off the printer echo /etc/rc.d/init.d/lpd stop # Stop the printer echo -e /nPlease turn the printer on now. echo Press return to continue read JUNK # Stall until the user turns the printer # back on echo # A blank line is printed /etc/rc.d/init.d/lpd start # Start the printer #!/bin/bash # Scriptname: tellme # Using the old-style test command echo -n How old are you? read age if [ $age -lt 0 -o $age -gt 120 ] then echo Welcome to our planet! exit 1 fi if [ $age -ge 0 -a $age -le 12 ] then echo A child is a garden of verses elif [ $age -ge 12 -a $age -le 19 ] then echo Rebel without a cause elif [ $age -ge 20 -a $age -le 29 ] then echo You got the world by the tail!! elif [ $age -ge 30 -a $age -le 39 ] then echo Thirty something... else echo Sorry I asked fi #!/bin/bash # Scriptname: tellme # Using the new (( )) compound let command echo -n How old are you? read age if (( age < 0 || age > 120 )) then echo Welcome to our planet! exit 1 fi if ((age >= 0 && age <= 12)) then echo A child is a garden of verses elif ((age >= 13 && age <= 19 )) then echo Rebel without a cause elif (( age >= 19 && age <= 29 )) then echo You got the world by the tail!! elif (( age >= 30 && age <= 39 )) then echo Thirty something... else echo Sorry I asked fi #!/bin/bash # Name:wholenum # Purpose:The Linux expr command tests that the user enters an # integer # echo Enter a number. read number if expr $number + 0 > /dev/null 2>&1 then : else echo You did not enter an integer value. exit 1 fi #!/bin/bash # Scriptname: xcolors echo -n Choose a foreground color for your xterm window: read color case $color in [Bb]l??) xterm -fg blue -fn terminal & ;; [Gg]ree*) xterm -fg darkgreen -fn terminal & ;; red | orange) # The vertical bar means or xterm -fg $color -fn terminal & ;; *) xterm -fn terminal ;; esac echo Out of case #!/bin/bash # Scriptname: backup # Purpose: # Create backup files and store them in a backup directory # dir=/home/jody/ellie/backupscripts for file in memo[1-5] do if [ -f $file ] then cp $file $dir/$file.bak echo $file is backed up in $dir fi done #!/bin/bash # Scriptname: dater # Purpose: set positional parameters with the set command # and shift through the parameters. set $(date) while (( $# > 0 )) # or [ $# -gt 0 ] do echo $1 shift done