1.if 是单分支语句,使用格式如下:
if condition ; then
statement
…..
fi
2.if … else 是双分支语句,使用格式如下:
if condition ; then
statement
….
else
statement
….
fi
3.if …elif…elif…else 是多分支语句,使用格式如下:
if condition ; then
statement
….
elif condition ; then
statement
…..
elif condition ; then
statement
…..
.
.
.
else
statement
….
fi
4.while 语句是循环语句,当条件满足的情况下才循环,不满足则退出循环,使用格式如下:
while condition ; do
statement
…..
done
5.until 语句也是循环语句,当条件不满足的情况下循环,满足则不循环,使用格式如下:
until condition ; do
statement
…..
done
6.case 也是循环语句,使用格式如下:
case $var(变量) ; in
value1)
……

value2)
…..

*)

..
..
..
esac

脚本练习:
1.计算100以内所有能被3整除的正整数的和。
#!/bin/bash
declare -i sum=0
for I in {1..100}; do
if [ $[$I%3] -eq 0 ]; then
let sum+=$I
fi
done
echo " the sum is :$sum"
2.计算100以内所有奇数的和以及所有偶数的和
#!/bin/bash
# echo "exercise"
declare -i sum1=0
declare -i sum2=0
for I in {1..100}; do
if [ $[$I%2] -eq 0 ]; then
let sum1+=$I
else
let sum2+=$I
fi
done
echo " the even sum is :$sum1"
echo " the oddnumber sum is :$sum2"
3.判断/var/log下的文件的类型:
如果是普通文件,则说明其为普通文件;
如果是目录文件,则说明其为目录文件;
如果是符号链接文件,则说明其为符号链接文件;
否则,说明文件类型无法识别;
#!/bin/bash
file1=/var/log/*
for file in $file1 ; do
if [ -f $file ]; then
echo "$file is common file"
elif [ -d $file ]; then
echo "$file is directory file"
else
echo "$file is unknow"
fi
done
4.写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为
/sbin/nologin的用户
并统计各类shell下的用户总数,显示结果形如:bash,3user,they
are:root,redhat,gentoo nologn,2user,they are:bin,ftp
#!/bin/bash
file=/etc/passwd
bsh='/bin/bash'
nobsh='/sbin/nologin'
use=`cat $file | cut -d: -f1`
declare -i d1=0
declare -i d2=0
for I in $use ; do
s=`grep "^$I:" $file | cut -d: -f7`
if [ "$s" = $bsh ] ; then
let d1=$d1+1
muser=$I\,$muser
elif [ "$s" = $nobsh ] ; then
let d2=$d2+1
suser=$I\,$suser
fi
done
echo "BASH,$d1 users ,they are:"
echo $muser
echo
echo "NOLOGIN,$d2 users ,they are:"
echo $suser
5.写一个脚本:
(1)如果不存在,就创建文件/tmp/maintenance;如果存在,就事先删除
(2)在文件/tmp/maintenance中添加如下内容:
172.16.0.6
172.16.0.17
172.16.0.20
(3)测试172.16.0.0/16网络内的所有主机是否在线,如果在线就显示其在线,如果此主机
在/tmp/maintenance文件中,就显示其正处于维护状态;否则,就显示其状态未知;
#!/bin/bash
file=/tmp/maintenace
if [ -e $file ]; then
rm -rf $file &> /dev/null
fi
touch $file
cat >> $file << EOF
172.16.0.6
172.16.0.17
172.16.0.20
EOF
bnet=172.16
for net in {0..254} ; do
for host in {1..254} ; do
if ping -c1 -W1 $bnet.$net.$host &> /dev/null ; then
echo "$bnet.$net.$host is up."
elif grep "$bnet.$net.$host$" $file &> /dev/null ;then
echo "$bnet.$net.$host is under maintenance."
else
echo "$bnet.$net.$host state is unknow."
fi
done
done
6写一个脚本,完成以下功能:
(1)、提示用户输入一个用户名;
(2)、显示一个菜单给用户,形如:
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
(3)、提醒用户选择一个选项,并显示其所选择的内容;如果用户给的是一个非上述所提示的选项,则提醒用户给出的选项错误,并请其重新选择后执行;
第一种方法:
#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo " Come on ,the user you input unexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your choice:" op
case $op in
U|u)
id -u $username;;
G|g)
id -g $username;;
S|s)
grep "^$username\>" /etc/passwd | cut -d: -f7;;
Q|q)
exit 8 ;;
*)
echo "input option wrong ,quit"
exit 9

esac
第二种方法:
#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo "Come on ,you input user notexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your option:" op
while true; do
case $op in
U|u)
id -u $username
break

G|g)
id -g $username
break

S|s)
grep "^$username\>" /etc/passwd | cut -d: -f7
break

Q|q)
exit 7 ;;
*)
read -p "Wrong option,Enter a right option:" op ;;
esac
done
7写一个脚本:
(1)、判断一个指定的脚本是否是语法错误;如果有错误,则提醒用户键入Q或者q无视错误并退出,其它任何键可以通过vim打开这个指定的脚本;
(2)、如果用户通过vim打开编辑后保存退出时仍然有错误,则重复第1步中的内容;否则,就正常关闭退出。
第一种方法
#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && exit 2
until bash -n $1 &> /dev/null ; do
read -p " Q|q to quit .others to edit:" opt
case $opt in
Q|q)
echo "quit..."
exit 3

*)
vim $1

esac
done
第二种方法:
#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && echo "Quit!" && exit 9
until bash -n $1 &> /dev/null ; do
read -p " Grammar wrong please enter Q|q to quit .others to edit:" opt
case $opt in
Q|q)
echo "quit..."
exit 3

*)
vim $1
bash -n $1 &> /dev/null
val=$?
[ "$val" -ne 0 ] && echo "xiu gai bu cheng gong. "

esac
done
第三种方法
#!/bin/bash
[ ! -f $1 ] && echo "Wrong scripts." && exit 4
bash -n $1 &> /dev/null
valu=$?
until [ $valu -eq 0 ] ; do
read -p "Q|q to quit ,others to edit:" op
case $op in
Q|q)
echo "Quit."
exit 9

*)
vim $1
bash -n $1 &> /dev/null
valu=$?

esac
done
8 写一个脚本:
查看redhat用户是否登录了系统,如果登录了,就通知当前脚本执行者“redhat
is logged on.”;否则,就睡眠5秒钟后再次进行测试;直到其登录为止退出;
第一种方法
#!/bin/bash
who | grep "^redhat\>" &> /dev/null
reval=$?
until [ $reval -eq 0 ] ;do
sleep 5
who | grep "^redhat\>" &> /dev/null
reval=$?
done
echo "redhat is logged on."
第二种方法:
#!/bin/bash
until who | grep "^redhat\>" &> /dev/null ; do
sleep 5
done
echo "redhat is logged on"
9写一个脚本:
(1)、向系统中添加20个用户,名字为linuxer1-linuxer20,密码分别为其用户名,要使用while循环;
(2)、要求:在添加每个用户之前事先判断用户是否存在,如果已经存在,则不再添加此用户;
(3)、添加完成后,显示linuxer1-linuxer20每个用户名及对应的UID号码和GID号码,形如 stu1, UID: 1000, GID: 1000
#!/bin/bash
declare -i I=1
while [ $I -le 20 ] ; do
l=linuxer$I
let I++
! id $l &> /dev/null && useradd $l &> /dev/null && echo "the user:$l" | passwd --stdin $l &> /dev/null && echo "a dd user $l successfully" || echo " The user $l is exit. "
d=`id -u $l`
g=`id -g $l`
echo " $l ,UID:$d,GID:$g "
done

学习shell脚本笔记的更多相关文章

  1. 菜鸟教程之学习Shell script笔记(上)

    菜鸟教程之学习Shell script笔记 以下内容是,学习菜鸟shell教程整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-shell.ht ...

  2. 学习 shell脚本之前的基础知识

    转载自:http://www.92csz.com/study/linux/12.htm  学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写sh ...

  3. 学习shell脚本之前的基础知识

    日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本的编写是必考的项目.有的单位 ...

  4. 菜鸟教程之学习Shell script笔记(下)

    菜鸟教程Shell script学习笔记(下) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...

  5. 30分钟快速学习Shell脚本编程

    什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...

  6. shell脚本笔记(原创不断记录)

    今天开始自己的shell脚本练习,刚好公司有太服务器,要时间对数据的cp是按月的: 考虑:首先寻找规律,发现都放置在/opt/www/aaa/  里面有很多的2级和3级目录和文件,但我追踪要备份的是年 ...

  7. 学习Shell脚本编程(第2期)_编写修改权限及执行Shell程序的步骤

    编写Shell程序 执行Shell程序 Shell程序有很多类似C语言和其他程序设计语言的特征,但是又没有程序语言那样复杂.Shell程序是指放在一个文件中的一系列Linux命令和实用程序.在执行的时 ...

  8. Shell脚本笔记

      如何查询文件里的某个字符串? grep “字符串” 文件 例:grep "abc" tmp.txt   如何将查询出来的内容赋给变量? str=$(grep "abc ...

  9. 别人的Linux私房菜(13)学习Shell脚本

    CentOS6.x以前版本的系统服务启动接口在/etc/init.d/目录下,存放了脚本. Shell脚本因调用外部命令和bash 的一些默认工具,速度较慢,不适合处理大量运算. 执行方式有:直接命令 ...

随机推荐

  1. mybatis传递参数到mapping.xml

    第一种方案 ,通过序号传递 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id ...

  2. Python解析命令行读取参数 -- argparse模块

    在多个文件或者不同语言协同的项目中,python脚本经常需要从命令行直接读取参数.万能的python就自带了argprase包使得这一工作变得简单而规范.PS:optparse包是类似的功能,只不过写 ...

  3. 网购vs实体店购物 [20161226]

    2016这一年依然网购了许多商品,比起以往,我选择退货的次数更多了. 以前如果网购到尺码或者样式不合适的东西,有时候将就拿去用,没有选择退货.由此闲置了不少衣物,而且延续高中时喜欢穿大一号的衣服的习惯 ...

  4. Django 静态文件配置(static files)

    Django version: 1.9 Python versrion: 3.5.2 这几天Django配置静态文件(本例是要加载index.css), 总是不对,最后终于试对了,这里记录下,方便以后 ...

  5. zz Must read

    http://www.opengpu.org/forum.php?mod=viewthread&tid=965&extra=page%3D1 游戏引擎剖析(Game Engine An ...

  6. vs2010 打包 SQL server compact 4.0 驱动程序

    sqlce 3.5应该是.net3.5环境下的.不知道最初的时候数据库的创建是用的3.5还是4.0 .这两天测试的时候,将4.0卸载了.就运行不上.报错为“未能加载文件或程序集“System.Data ...

  7. MYSQL中UNIX时间戳与日期的转换

    select FROM_UNIXTIME(1464973385.641,'%Y-%m-%d %H:%i:%s'); select UNIX_TIMESTAMP('2016-06-04 01:03:05 ...

  8. AIDL小结

    AIDL : Android Interface Define Language(接口定义语言) Service中跨进程间通信利器.... 一般都会有Client端和Server端(Server端提供 ...

  9. 使用dom4j解析XML

    jar包:dom4j //使用dom4j解析返回的xml SAXReader reader = new SAXReader(); Document doc = reader.read(new Byte ...

  10. Android中editText使用报错

    在activity_main.xml文件中添加了editText控件 <EditText        android:id="@+id/edit_text"        ...