[root@desktop27 mnt]# vim userfile
[root@desktop27 mnt]# vim passfile
[root@desktop27 mnt]# cat userfile
user1
user2
user3
[root@desktop27 mnt]# cat passfile
user1123
user2123
user3123
[root@desktop27 mnt]# vim create_user.sh
[root@desktop27 mnt]# cat create_user.sh #!/bin/bash
MAX_LINE=`wc -l$1 | cut -d" "-f1`
for LINE_NUM in `seq 1$MAX_LINE`
dosed -n "${LINE_NUM}p"$1sed -n "${LINE_NUM}p"$2done
[root@desktop27 mnt]# sh create_user.sh userfile passfile
user1
user1123
user2
user2123
user3
user3123
[root@desktop27 mnt]# vim create_user.sh
[root@desktop27 mnt]# cat create_user.sh #!/bin/bash
MAX_LINE=`wc -l$1 | cut -d" "-f1`
for LINE_NUM in `seq 1$MAX_LINE`
doUSERNAME=`sed -n "${LINE_NUM}p"$1`PASSWORD=`sed -n "${LINE_NUM}p"$2`useradd $USERNAMEecho$PASSWORD | passwd --stdin $USERNAMEdone
[root@desktop27 mnt]# sh create_user.sh userfile passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@desktop27 mnt]#
前一腳本的改進:若沒有跟兩個文件參數,將報錯
[root@desktop27 mnt]# vim create_user.sh
[root@desktop27 mnt]# cat create_user.sh #!/bin/bash
[ "$#"-ne"2" ]&&{echo-e"\033[31mERROR:please input userfile and passwordfile\033[0m"exit1
}
MAX_LINE=`awk 'BEGIN{N=0}{N++}END{print N}'$1`
for LINEMAX in `seq 1$MAX_LINE`
doUSERNAME=$(sed -n "${LINEMAX}P"$1)PASSWORD=$(sed -n "${LINEMAX}P"$2)useradd $USERNAMEecho$PASSWORD | passwd --stdin $USERNAMEdone
[root@desktop27 mnt]# sh create_user.sh
ERROR:please input userfile and passwordfile
[root@desktop27 mnt]# sh create_user.sh userfile
ERROR:please input userfile and passwordfile
[root@desktop27 mnt]# sh create_user.sh userfile passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@desktop27 mnt]#
再次改進:實現交互
[root@desktop27 mnt]# vim create_user.sh
[root@desktop27 mnt]# cat create_user.sh #!/bin/bashread -p "Please input userfile:" USER
[ -e"$USER" ]||{echo-e"\033[31mERROR: $USER is not exist!!\033[0m"exit1
}
read -p "Please input passwordfile:" PASSWORDFILE
[ -e"$PASSWORDFILE" ]||{echo"\033[31mERROR: $PASSWORDFILE is not exist!!\033[0m"exit1
}
MAX_LINE=`awk 'BEGIN{N=0}{N++}END{print N}'$USER`
for LINEMAX in `seq 1$MAX_LINE`
doUSERNAME=$(sed -n "${LINEMAX}P"$USER)PASSWORD=$(sed -n "${LINEMAX}P"$PASSWORDFILE)useradd $USERNAMEecho$PASSWORD | passwd --stdin $USERNAMEdone
[root@desktop27 mnt]# sh create_user.sh
Please input userfile:userfile
Please input passwordfile:passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@desktop27 mnt]#
腳本中的函數
腳本中的函數是把一個復雜的語句塊定義成一個字符串的方法 例如:
例一
[root@desktop27 mnt]# vim file.sh
[root@desktop27 mnt]# cat file.sh #!/bin/bashPING()
{read -p "Please input a idaddress: " IPping -c1 -w1 $IP &> /dev/null && echo$IP is up || echo$IP is downPING
}
PING
[root@desktop27 mnt]# sh file.sh
Please input a idaddress: 172.25.254.127172.25.254.127 is up
Please input a idaddress: 172.25.254.227172.25.254.227 is down
Please input a idaddress: 172.25.254.50172.25.254.50 is up
Please input a idaddress: ^C
[root@desktop27 mnt]# ##因為腳本中沒有退出設定,所以 crtl+c 退出
例一改進
[root@desktop27 mnt]# vim file.sh
[root@desktop27 mnt]# cat file.sh #!/bin/bashPING()
{read -p "Please input a idaddress: " IP[ "$IP" = exit ]&&{echo byeexit0}ping -c1 -w1 $IP &> /dev/null && echo$IP is up || echo$IP is downPING
}
PING
[root@desktop27 mnt]# sh file.sh
Please input a idaddress: 172.25.254.127172.25.254.127 is up
Please input a idaddress: exit
bye
[root@desktop27 mnt]#
例二
[root@desktop27 mnt]# vim file_check.sh
[root@desktop27 mnt]# cat file_check.sh #!/bin/bashFILE_CHECK()
{[ "$1""$2" ]&&{echo$2 is $3exit0}
}
[ -e"$1" ]||{echo$1 is not exist!!echo0
}
FILE_CHECK -L $1 link
FILE_CHECK -f$1 file
[root@desktop27 mnt]# sh file_check.sh /etc/system-release
/etc/system-release is link
[root@desktop27 mnt]# sh file_check.sh /etc/passwd
/etc/passwd is file
[root@desktop27 mnt]#