test1:for循环
#!/bin/bash for var in one two three four do echo "The number is $var" done echo "Now show read values from file" file="place" for var in $(cat $file) do echo "Visit beautiful $var" done cat place: Tokyo New York HongKong Landon 结果: The number is one The number is two The number is three The number is four Now show read values from file Visit beautiful Tokyo Visit beautiful New Visit beautiful York Visit beautiful HongKong Visit beautiful Landontest2: shell脚本中,对于循环的输出、可以使用管道或进行重定向。可以通过done命令之后添加一个处理命令来实现。
#!/bin/bash #注意for后面的括号 for (( a = 1; a < 10; a++ )) do echo "The number is $a" done > test14test.txt echo "Command is finished!" cat test14test.txt The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 结果: Command is finished!循环是对系统数据进行迭代最常用的方法,无论是目录中的文件还是文件中的数据,下面是一些简单的例子(example1、example2)。
example1:查找可执行文件。使用命令行运行一个程序时,Linux系统会搜索一系列目录来查找对应的文件,这些目录被定义在PATH目录中。如果想找出那些可执行文件是可用的,只需要扫描PATH环境变量中所有目录就行了,这个脚本就是做这件事。
IFS分隔符,即内部字段分隔符,默认情况下会将 空格、制表符、换行符当做字段分隔符。如果bash shell在数据中看到了这些字符中的任意一个,就会假定这表明了列表中一个新数据字段的开始。要解决这个问题,可以在shell脚本中临时更改IFS的值来限制分隔符的字符。例如:IFS=$'\n' (换行符)即:shell忽视空格和制表符,把换行当成新数据字段的开始。
如果冒号作为分隔符,则:IFS=:
如果指定多个分隔符(换行、冒号、分号、双引号),则:IFS=$'\n':;"
#!/bin/bash #1. 第一个for循环,对环境变量PAHT目录进行迭代.即:找到所有的目录 IFS=: for folder in $PATH do echo "Now print all folder:$folder:" #2. 这样所有的目录都放在了folder中,第二个for用来遍历目录下的所有文件 for file in $folder/* do #3. 最后检查每个文件是否具有可执行权限 if [ -x $file ] then echo "Now print all can excult file:$file" fi done done 结果: Now print all folder:/usr/local/sbin/uds/bin: Now print all can excult file:/usr/local/sbin/uds/bin/conf.ini Now print all can excult file:/usr/local/sbin/uds/bin/Guide Now print all can excult file:/usr/local/sbin/uds/bin/libcrypto.so.1.0.0 Now print all can excult file:/usr/local/sbin/uds/bin/libssl.so.1.0.0 Now print all can excult file:/usr/local/sbin/uds/bin/libuds.so ... ... ... ...
example2:创建多个用户账户。拥有大量用户时,为每一个用户创建账户,可以使用while循环。前提是需要把所有用户放在文本文件中,然后使用脚本创建。这个文本文件的格式:userid,username。用逗号分隔。读取文件数据时,将IFS设置成逗号,然后使用read命令读取文件中的各行。如;while IFS=',' read -r userid name
read命令会自动读取.csv文件的下一行内容,当read命令返回FALSE(读取完整个文件时),while命令就退出了。
运行后,命令:tail /etc/passwd,发现用户已经添加进去了。
#!/bin/bash input="users.csv" while IFS=',' read -r userid username do echo "adding user:$userid" useradd -c "$name" -m "$userid" done < "$input" cat user.csv xt,Xing ab,AngleBaby jj,Junjie 结果: adding user:xt useradd:用户“xt”已存在 adding user:ab useradd:用户“ab”已存在 adding user:jj useradd:用户“jj”已存在test3:演示引用库文件(自定义的库文件)。
#!/bin/bash #myFunc库与该脚本在同一个文件夹,因此可以使用下面的形式 . ./myFuncs val1=10 val2=20 result1=$(addem $val1 $val2) result2=$(multem $val1 $val2) echo "The addem result is $result1" echo "The multem result is $result2" cat myFuncs #!/bin/bash #自定义库文件 #注意,函数名和{之间一定要有空格,不然会说找不到这个函数 function addem { echo $[ $1 + $2 ] } function multem { echo $[ $1 * $2 ] } 结果: The addem result is 30 The multem result is 200test4:正则表达式实战 - 目录文件计数。
#!/bin/bash #统计PATH路径的各个目录下的文件数目 mypath=$(echo $PATH | sed 's/:/ /g') count=0 for dir in $mypath do check=$(ls $dir) for item in $check do count=$[ $count + 1 ] done echo "$dir - $count" count=0 done 结果: /usr/local/sbin/uds/bin - 15 ls: 无法访问'/usr/local/arc/arcanist/bin': No such file or directory /usr/local/arc/arcanist/bin - 0 ls: 无法访问'/usr/bin/php/bin': Not a directory /usr/bin/php/bin - 0 ls: 无法访问'/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/bin': No such file or directory /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/bin - 0 ls: 无法访问'/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/jre/bin': No such file or directory /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/jre/bin - 0 /usr/lib64/qt-3.3/bin - 0 /usr/lib64/ccache - 9 /usr/local/bin - 2 /usr/bin - 2226 /usr/local/sbin - 2 /usr/sbin - 712 ls: 无法访问'/home/10237378@zte.intra/.local/bin': No such file or directory /home/10237378@zte.intra/.local/bin - 0 ls: 无法访问'/home/10237378@zte.intra/bin': No such file or directory /home/10237378@zte.intra/bin - 0