一、创建新脚本,可添加信息
#!/bin/bash
while getopts ":a:d:" SWITCH; do
case $SWITCH in
a)
AUTHOR=$OPTARG ;;
d)
DESC=$OPTARG ;;
?)
echo "Unkown options."
exit 1 ;;
esac
done
shift $[$OPTIND-1]
if ! grep "#!/bin/bash" $1 &> /dev/null; then
cat >> $1 << EOF
#!/bin/bash
#Description: ${DESC}
#Date: `date "%F %T"`
#Author:${AUTHOR:=Jack}
#Version:
EOF
fi
vim 7 $1
wait
chmod u x $1
注:1.here document: <<
cat >> FILE << EOF
2.给newscript权限并放到/bin下
二、三个数比较,得出最大数
方法1:
6 read -p "First number:" A
7 read -p "Second number:" B
8 read -p "Third number:" C
9
10 if [ $A -ge $B ]; then
11 MAX=$A
12 else
13 MAX=$B
14 fi
15 if [ $MAX -le $C ];then
16 MAX=$C
17 fi
18
19 echo "The max is $MAX."
方法2:
6 read -p "First number:" A //A前面要有空格
7 read -p "Second number:" B
8 read -p "Third number:" C
9
10 if [ $A -ge $B ]; then
11 if [ $A -ge $C ]; then
12 echo "The max is $A."
13 else
14 echo "The max is $C."
15 fi
16 else
17 if [ $B -ge $C ]; then
18 echo "The max is $B."
19 else
20 echo "The max is $C."
21 fi
22
23 fi
三、求1-100偶数之和
8 let I=1
9 let SUM=0
10
11 until [ $I -gt 100 ]; do
12 if [ $[$I%2] -eq 0 ]; then 或 [ $[$I%2] -eq 0 ] && let SUM =$I //$[$I%2]注意
13 let SUM =$I
14 fi
15 let I
16 done
17
18 echo "The sum is $SUM."
四、给一个用户,判断用户是否存在并输出其shell
read -p "Please give a username:" MYUSER
if cut -d: -f1 /etc/passwd | grep "^$MYUSERcontentquot; &> /dev/null; then
MYBASH=`grep "^$MYUSER:" /etc/passwd | cut -d: -f7`
echo "${MYUSER}''s shell is $MYBASH."
else
echo "You fool,no $MYUSER."
exit 4
fi
五、输入一文件路径,判断有几行
read -p "Give a file path:" MYFILE
if [ -e $MYFILE -a -f $MYFILE ]; then
let C=0
while read LINE; do
let C
echo $LINE | tr ''a-z'' ''A-Z''
done < $MYFILE
echo "There are $C lines in $MYFILE."
else
exit 5
fi
六、给一个用户,判断此用户登录次数
read -p "A user:" MYUSER
cut -d: -f1 /etc/passwd | grep "^$1contentquot; &> /dev/null || exit 6
let COUNT=`who | grep "^$1" | wc -l`
until [ $COUNT -ge 4 ]; do
sleep 5
let COUNT=`who | grep "^$1" | wc -l`
done
echo "$MYUSER login 4 times."
七、修改ifcfg-eth0文件
法1:
IFFILE=''/root/ifcfg-eth0''
read -p "IPaddr[192.168.0.1]:" MYIP
[ -z $MYIP ] && MYIP=''192.168.0.1''
read -p &quo
|