[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`
do
sed -n "${LINE_NUM}p"$1
sed -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`
do
USERNAME=`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`
do
USERNAME=$(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`
do
USERNAME=$(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: " IP
ping -c1 -w1 $IP &> /dev/null && echo$IP is up || echo$IP is down
PING
}
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 bye
exit0
}
ping -c1 -w1 $IP &> /dev/null && echo$IP is up || echo$IP is down
PING
}
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]#