bash脚本编程实例
-
1.写一个脚本
- 接受一个以上文件路径作为参数
- 显示每个文件拥有的行数
-
总结说明本次共为几个文件统计了其行数
#!/bin/bash # read -p "please input some paths:" paths if [ -z $paths ];then echo "There are not any paths inputting." exit 1 fi declare -i files=0 for i in $paths;do if [ ! -e $i ];then echo "$i is not existing." continue elif [ ! -f $i ];then echo "$i is not a file" continue else count=`wc -l $i|cut -d' ' -f2` echo "$i has $count lines." files=$[$files+1] fi done echo "counting lines for $files files."
-
2.写一个脚本
- 传递两个以上字符串当做用户名
- 创建这些用户,且密码同为用户名
-
总结说明共创建了几个用户
#!/bin/bash # read -p "please input some usernames:" usernames if [ -z $usernames ];then echo "There are not any usernames inputting." exit 1 fi declare -i users=0 for i in $usernames;do if ! id $i &> /dev/null;then useradd $i echo $i|passwd --stdin $i users=$[$users+1] else echo "$i has existed." continue fi done echo "createing users for $users users."
-
3.写一个脚本,新建20个用户,visitor1-visitor20,计算他们的ID之和
#!/bin/bash # declare -i ids=0 for i in {1..20};do if ! id visitor$i &> /dev/null;then useradd visitor$i echo "create a new user named visitor$i." else echo "this user named visitor$i has existed." fi id=`id -u visitor$i` ids=$[$ids+$id] done echo "id count for these users are $ids."
原创文章,作者:N27_xiaoni,如若转载,请注明出处:http://www.178linux.com/84388
评论列表(1条)
这几个脚本还是用到蛮多知识点的,很不错,再接再励。