您的位置:首页 > 运维架构 > Shell

Linux shell 脚本实例四 By 人生百味 发表于 2006-3-7 13:20:00

2008-10-28 23:44 381 查看
#!/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,write,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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: