练习一:写一个脚本
.设定变量FILE的值为/etc/passwd
.依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么
形如:(提示:LINE=`wc -l /etc/passwd | cut -d" " -f1`)
Hello,root,your UID is .
.统计一个有多少个用户
答案一:#!/bin/bash
file="/etc/passwd"
LINES=`wc -l $file | cut -d" " -f1`
for I in `seq $LINES`;do
userid=`head -$I $file | tail - |cut -d: -f3`
username=`head -$I $file | tail - |cut -d: -f1`
echo "hello $username,your UID is $userid"
done
echo "there are $LINES users"
答案二:#!/bin/bash
file=/etc/passwd
let num=
for I in `cat $file`;do
username=`echo "$I" | cut -d: -f1`
userid=`echo "$I" | cut -d: -f3`
echo "Hello,$username,your UID is $userid"
num=$[$num+]
done
echo "there are $num users"
练习二:写一个脚本
.切换工作目录至/var
.依次向/var目录中的每个文件或子目录问好,形如:
(提示:for FILE in /var/*;或for FILE in `ls /var`;)
Hello,log
3.统计/var目录下共有多个文件,并显示出来
答案:#!/bin/bash
cd /var
let num=0
for I in `ls /var/*`;do
echo "hello $I"
num=$[$num+1]
done
echo "the number of files is $num"
练习三:写一个脚本
1.设定变量file的值为/etc/passwd
2.使用循环读取文件/etc/passwd的第2,4,6,10,13,15行,并显示其内容
3.把这些行保存至/tmp/mypasswd文件中
答案:#!/bin/bash
file="/etc/passwd"
for I in 2 4 6 10 13 15;do
exec 3>/tmp/mypasswd
line=`head -$I $file | tail -1`
echo "$line"
echo "$line" >&3
exec 3>&-
done
练习四:写一个脚本
传递两个整数给脚本,让脚本分别计算并显示这两个整数的和,差,积,商
答案如下:vim test.sh
#!/bin/bash
echo "first number $1" (表示输出第一个数)
echo "second number $2" (表示输出第二个数)
echo " $(($1+$2))" (输出两数之和)
echo "$[$1-$2]" (输出两数之差)
echo "$[$1*$2]" (输出两数之积)
echo "$[$1/$2]" (输出两数之商)
:wq (表示保存并退出vi编辑器)
chmod +x test.sh (给test.sh执行的权限)
./test.sh 2 3 (传递两个参数并执行脚本
作业一:写一个脚本:
1.创建目录/tmp/scripts
2.切换工作目录至此目录中
3.复制/etc/pam.d目录至当前目录,并重命名为test
4.将当前目录的test及其里面的文件和子目录的属主改为redhat
5.将test及其子目录中的文件的其它用户的权限改为没有任何权限
答案:
#!/bin/bash
mkdir -v /tmp/scripts
cd /tmp/scripts
cp -r /etc/pam.d ./test
chown -R redhat ./test
chmod -R o=--- ./test
作业二:写一个脚本
1.显示当前系统日期和时间,而后创建目录/tmp/lstest
2.切换工作目录至/tmp/lstest
3.创建目录a1d,b56e,6test
4.创建空文件xy,x2y,732
5.列出当前目录下以a,x或者6开头的文件或目录
6.列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录
答案:
#!/bin/bash
date
mkdir -pv /tmp/lstest
cd /tmp/lstest
mkdir a1d b56e 6test
touch xy x2y 732
ls [ax6]*
ls [[:alpha:]][[:digit:]]* 作业三:写一个脚本
添加10个用户user1到user10,但要求只有用户不存在的情况下才能添加
答案:
#!/bin/bash
for I in `seq 1 10`;do
cut -d: -f1 /etc/passwd |grep "user$I" 2>>/tmp/etc.err || useradd user$I
done
作业四:写一个脚本
通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线
如果在线,就显示“ip is up”
如果不在线,就显示“ip is down”
答案: #!/bin/bash
for I in `seq 151 254`;do
ping -c1 -w1 192.168.0.$I &>/dev/null && echo "192.168.0.$I is up" || echo "192.168.0.$I is down"
done

shell脚本例子集锦(习题总结)的更多相关文章

  1. Linux Shell 高级编程技巧4----几个常用的shell脚本例子

    4.几个常用的shell脚本例子    4.0.在写脚本(同样适用在编程的时候),最好写好完善的注释    4.1.kill_processes.sh(一个杀死进程的脚本) #!/bin/bash c ...

  2. shell脚本小集锦

    1) 如何向脚本传递参数 ? ./script argument 例子: 显示文件名称脚本 ./show.sh file1.txt cat show.sh #!/bin/bash 2) 如何在脚本中使 ...

  3. Shell脚本例子集合

    # vi xx.sh 退出并保存 # chmod +x xx.sh # ./xx.sh -2. 调试脚本的方法 # bash -x xx.sh 就可以调试了 . -1. 配置 secureCRT 的设 ...

  4. shell脚本例子

    #!/bin/sh   str="####" echo $1 | grep $str 1>/dev/null if [ `echo $?` -eq 0 ] then   ec ...

  5. Linux Shell编程第1章——Shell脚本编程概述

    目录 Linux和Shell简介 Linux是一套可免费使用和自由传播的类UNIX操作系统.Shell是一种具备特殊功能的程序,它提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令,并把它送 ...

  6. Shell脚本之:for

    与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...

  7. shell脚本中执行sql的例子

    这个例子演示了如何在shell脚本中执行多个sql来操作数据库表. #! /bin/sh USER_HOME=/home/`whoami` . /etc/profile if [ -f ${USER_ ...

  8. java调用shell脚本,并获得结果集的例子

    /** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...

  9. shell脚本简单例子

    eg: Expect: 1.用环境变量RANDOM随机生成一个100以内的随机数 2.read读取当前输入 3.当前输入对比随机生成的数 4.当两个数相等时跳出苏循环,并计数(比较n次结果才相等) # ...

随机推荐

  1. Eclipse下Ruby的配置]

      简述: 在Eclipse中开发Ruby开发环境   步骤: 第一步, 1. 在Eclipse的Help ->  Install New Software输入 http://download. ...

  2. myeclipse项目里有红色感叹号

    myeclipse项目里有红色感叹号 这种情况是因为 .classpath 文件里面配置引用了某个jar,但是实际上你的 lib 里面并没有这个jar 所以才会有红色的提示. 不用拿.classpat ...

  3. URAL 1161 Stripies(数学+贪心)

    Our chemical biologists have invented a new very useful form of life called stripies (in fact, they ...

  4. mac office

    弄了个office2016正式版的教程,大多数朋友表示搞不懂,SO重新写了个超级详细的,在不懂我也是醉了.Mac office 2016免费安装教程微软近日正式向 Office 365 订阅用户发布了 ...

  5. Spring容器中的Bean

    一,配置合作者的Bean Bean设置的属性值是容器中的另一个Bean实力,使用<ref.../>元素,可制定一个bean属性,该属性用于指定容器中其他Bean实例的id属性 <be ...

  6. PTPX中的report 选项

    Report的生成 report_power表示产生power report,update_power表示进行power analysis. report_power命令可以生成四种形式的report ...

  7. [Sinatra、Mongo] Mongo

    Mongo is a document-oriented database. Install the required gems: gem install mongo gem install bson ...

  8. 一个容易被忽略的ReportingService超时问题

    我们在使用Sql Server Reporting Service开发报表的时候,经常会遇到报表超时的问题,报表超时的原因有很多,也有很多地方可以设置报表的超时时间,比如在报表中的数据源(dataso ...

  9. some software that is used to speed up your system

    1.RAMDISK take some space in ram and use them as the disk. Primo Ramdisk Server Edition 5.6.0 regist ...

  10. Spark实战1:shell+独立App使用总结

    Spark改进了Hadoop执行非流式算法的需要多次IO的缺陷,Spark的所有操作都是基于RDD弹性分布式数据集这种数据结构的,对RDD的操作主要的操作包括transform和action两种操作. ...