一、创建新脚本,可添加信息
#!/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 "^$MYUSER$" &> /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 "^$1$" &> /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 "Netmask[255.255.255.0]:" MYMASK
[ -z $MYMASK ] && MYMASK='255.255.255.0'
if grep -E "^BOOTPROTO=(dhcp)|(bootp)$" $IFFILE &> /dev/null; then
sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $IFFILE
grep "^IPADDR=.*" $IFFILE &> /dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \IPADDR=$MYIP" $IFFILE
grep "^NETMASK=.*" $IFFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYMASK/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \NETMASK=$MYMASK" $IFFILE
fi
法2:
IFFILE='/root/ifcfg-eth0'
read -p "IPaddr[192.168.0.1]:" MYIP
[ -z $MYIP ] && MYIP='192.168.0.1'
#read -p "Netmask[255.255.255.0]:" MYMASK
#[ -z $MYMASK ] && MYMASK='255.255.255.0'
echo "Please choose an option[1,2,3]:"
cat << EOF
1) 255.255.255.0
2) 255.255.0.0
3) 255.0.0.0
EOF
read CHOICE
case $CHOICE in
'1')
echo "You choose 255.255.255.0."
'2')
echo "You choose 255.255.0.0."
'3')
echo "You choose 255.0.0.0."
'*')
echo "You choose nothing."
esac
[ -z $MYMASK ] && MYMASK='255.255.255.0'
if grep -E "^BOOTPROTO=(dhcp)|(bootp)$" $IFFILE &> /dev/null; then
sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $IFFILE
grep "^IPADDR=.*" $IFFILE &> /dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \IPADDR=$MYIP" $IFFILE
grep "^NETMASK=.*" $IFFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYMASK/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \NETMASK=$MYMASK" $IFFILE
fi
法3:
IFFILE='/root/ifcfg-eth0'
read -p "IPaddr[192.168.0.1]:" MYIP
[ -z $MYIP ] && MYIP='192.168.0.1'
#read -p "Netmask[255.255.255.0]:" MYMASK
#[ -z $MYMASK ] && MYMASK='255.255.255.0'
echo "Please choose an option[1,2,3]:"
cat << EOF
1) 255.255.255.0
2) 255.255.0.0
3) 255.0.0.0
EOF
read CHOICE
case $CHOICE in
'1')
echo "You choose 255.255.255.0."
'2')
echo "You choose 255.255.0.0."
'3')
echo "You choose 255.0.0.0."
'*')
[ -z $CHOICE ] && read -p "Plsase input your mask:" MYMASK || MYMASK="255.255.255.0"
esac
[ -z $MYMASK ] && MYMASK='255.255.255.0'
if grep -E "^BOOTPROTO=(dhcp)|(bootp)$" $IFFILE &> /dev/null; then
sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $IFFILE
grep "^IPADDR=.*" $IFFILE &> /dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \IPADDR=$MYIP" $IFFILE
grep "^NETMASK=.*" $IFFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYMASK/g" $IFFILE || sed -i "/BOOTPROTO=.*/a \NETMASK=$MYMASK" $IFFILE
fi
八、case用法
cat << EOF
1) 255.255.255.0
2) 255.255.0.0
3) 255.0.0.0
EOF
read CHOICE
case $CHOICE in
'1') //1不加''则会被认为字符
echo "You choose 255.255.255.0."
'2')
echo "You choose 255.255.0.0."
'3')
echo "You choose 255.0.0.0."
'*')
echo "You choose nothing."
esac
九、增加用户,参数选择
NUID=$[`cut -d: -f3 /etc/passwd | grep -v "65534" |sort -n|tail -1`+1]
NGID=$[`cut -d: -f3 /etc/group |grep -v "65534" |sort -n | tail -1`+1]
TODAY=$[`date +"%s"`/86400]
usage() {
echo -e "\033[31mUsage:\033[0m `basename $0` [-u UID] username"
}
while getopts ":u:" SWITCH; do
case $SWITCH in
u)
NEWUID=$OPTARG
cut -d: -f3 /etc/passwd |grep "^$NEWUID" &> /dev/null && echo "UID exists." && exit 1 ;;
\?)
usage exit 1 ''
esac
done
shift $[$OPTIND-1]
if ! cut -d: -f1 /etc/group |grep "^$1" &> /dev/null; then
echo "$1:x:$NGID:" >> /etc/group
else
echo "The GROUP $1 exists."
exit 1
fi
if ! cut -d: -f1 /etc/passwd |grep "^$1" &> /dev/null; then
echo "$1:x:${NEWUID:=$NUID}:$NGID::/home/$1:/bin/bash" >> /etc/passwd
echo "$1:!!:$TODAY:0:99999:7:::" >> /etc/shadow
else "The user $1 exists."
exit 1
fi
cp -r /etc/skel /home/$1
chown -R $1:$1 /home/$1
chmod -R go=--- /home/$1
十、捕捉信号
1.
touch /tmp/traptest
mytrap() {
rm -f /tmp/traptest &> /dev/null
echo "Quid the script..."
exit 1
}
trap 'mytrap' SIGINT
read A
2.
trap 'echo "Quit..."; exit 1' SIGINT
for I in {1..25}; do
ping -W2 -c1 192.168.0.$I &> /dev/null
[ $? -eq 0 ] && echo "This station is online." || echo "This station is outline."
done
十一、数组脚本
grp1=(a b c d)
grp2=(e f g h i)
INDEX=$[$RANDOM%4]
echo ${grp2[$INDEX]}
十二、获取独立分区
TMP1=`mktemp /tmp/disk.XXXXXX`
df -l|grep -v "^/dev/[^[:space:]]*[[:space:]]*$" |cut -d'%' -f2 |grep -v -E "Mounted|/dev" > $TMP1
show_menu() {
let I=1
echo "Please choose an choice."
while read LINE; do
echo "$I $LINE"
let I++
done < $TMP1
}
show_memu
read -p "Your choice('Q' for quit):" CHOICE
select_line() {
MYLINE=`head -n $1 $TMP1 |tail -1`
df -lh|grep "$MYLINE$"
}
while [ $CHOICE!='Q' ]; do
case $CHOICE in
1) select_line 1 ;;
2) select_line 2 ;;
3) select_line 3 ;;
Q) exit 0 ;;
esac
read -p "Your choice('Q' for quit):" CHOICE
done
wait
rm -f $TMP1
十三、压缩文件
mycompress() {
case $1 in
'gzip') gzip ic $2 > ${2}.gz ;;
'bzip2') bzip2 -k $2 ;;
'zip') zip ${2}.zip $2 ;;
*) echo "Wrong Compress format." ; exit 1 ;;
esac
}
while getopts ":c:" SWITCH; do
case $SWITCH in
'c')
CTOOL=$OPTARG ;;
\?)
echo "Wrong Option"
echo "^[[31mUsage:^[[0m `basename $0` -c zip|gzip|bzip2 file_name"
exit 1 ;;
esac
done
shift $[$OPTIND-1]
mycompress $CTOOL $1
十四、提示用户修改密码
[ ! $USER='root' ] && echo "Only root can execute this script." && exit 1
MYFILE='/etc/shadow'
TODAY=$[`date +"%s"`/86400]
if [ -e $MYFILE ]; then
while read LINE; do
USERNAME=`echo $LINE |cut -d: -f1`
let MAXDAY=`echo $LINE | cut -d: -f5`
[ $[$MAXDAY-$TODAY] -lt 10 ] && echo "${USERNAME}'s password need to change."
done < $MYFILE
fi
十五、
let LINENUM=0
usage() {
echo "Unkown options,Usage:`basename $0` [-n] filename"
}
while getopts ":n:" SWITCH; do
case $SWITCH in
n)
let LINENUM=1
MYARG=$OPTARG ''
\?)
usage
exit 1 ;;
esac
done
shift $[$OPTIND-1]
let I=1
while read LINE; do
if [ $LINENUM -eq 1 ]; then
echo "$I $LINE"
let I++
else
echo "$LINE"
fi
done < $1
十六、查看磁盘空间
usage() {
echo "Wrong Option."
echo "Usage: `basename $0` [-t] [-u] [-f]"
}
while getopts ":tuf" SWITCH; do
case $SWITCH in
t) echo "The Total is:`free -m | grep "^Mem" |awk '{print $2}'`" ;;
u) echo "The Used is:`free -m |grep "^Mem" | awk '{print $3}'`" ;;
f) echo "The Free is: `free -m | grep "^Mem" | awk '{print $4}'`" ;;
\?) usage; exit 1 ;;
esac
done
十七、ping B、C类网
cping() {
PINGNET=`echo $1 |sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/g'`
let I=1
while [ $I -le 20 ]; do
ping -c1 -W1 $PINGNET.$I &> /dev/null
[ $? -eq 0 ] && echo "$PINGNET.$I online." || echo "$PINGNET.$I offline."
let I++
done
}
bping() {
BPINGNET=`echo $1 | sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\)\..*/\1/g'`
let K=0
while [ $K -le 254 ]; do
cping $BPINGNET.$K
let K++
done
}
read -p 'A network:' MYNET
bping $MYNET