shell脚本入门语法与样例

一、shell脚本语法糖:

二、样例代码

问题1:写一个脚本,判断当前所用的shell

#!/bin/bash
#注意if的空格以及[]里面的空格这里错了半天啊
if [ "$SHELL" = "/bin/bash" ];then
echo "your login shell is the bash \n"
echo "SHELL is : $SHELL"
else 
echo "your login shell is not bash but $SHELL"
fi

if 语句语法

if[];then

elif[];then

else

fi

问题2:写一个脚本使其从一个文件里面读入有echo的语句,并把其写在本文件末尾

#!/bin/bash
mailfolder=/data/test.txt
if [ -r "$mailfolder" ];then
    echo "$mailfolder has massage from : "
    echo | grep '^echo ' $mailfolder >> readme2.txt
    chmod +r readme2.txt
    cat readme2.txt >> $mailfolder
    rm -f readme2.txt
else
    echo "Can not read $mailfolder"
    touch $mailfolder
    chmod +rw $mailfolder
    echo "echo 人生自古谁无死,六区蛋清找旱情! " >> $mailfolder
fi

问题3:依次向/etc/passwd中的每个用户问好,并且说出对方的ID什么(Hello,root,your UID is0.)此处不建议使用for变量--for是根据空格取值

#!/bin/bash
fl=/etc/passwd
count=`cat $fl | wc -l`
#下面是一个管道,下面循环读文件中的每一行
cat $fl |
while read line
do
    user=`echo $line|awk -F ':' '{print $1}'`
    #代表以 :分段$1就是取第1段
    uid=`echo $line|awk -F ':' '{print $3}'`
    echo "hello, $user Your UID is $uid"
done
#输出前面求得的用户数
echo "====User_count:$count===="

问题4:依次向/tmp目录中的每个文件或子目录问好(Hello,log),此处用for循环

#!/bin/bash
for i in /tmp/*
do
    echo "Hello , $i"
done
count=`ls -l|grep '^-'|wc -l`
echo "====file_count:$count===="

问题5:传递两个整数给脚本,让脚本分别计算并显示这两个整数的和,差,积,商

#!/bin/bash
a=1
b=2
echo "$a + $b = $(($a+$b))"
echo "$a - $b = $(($a - $b))"
echo "$a * $b = $(($a*$b))"
echo "$a / $b = $(($a/$b))"

问题6:求1到100的和?

#!/bin/bash
sum=0
i=1
while [ $i -le 100 ]
do
    sum=$(($sum + $i))
    i=$(($i+1))
done
echo "$sum"

问题7:写个很实用的脚本吧,假设你现在在机房,你想要看看机房哪些ip在开着,方便咱。。。,是吧,我们可以通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线

#!/bin/bash
for i in `seq 2 254`
do
    #-c可以指定ping的次数,-w测试的时间 -w 1就是1秒中无论成功失败都结束
    #-c -w后面都要输入参数所以都要带上l
    ping -c1 -w1 192.168.51.$i > /dev/null 2>&1
    #$?代表最后命令退出状态,0代表没错,其他代表有错
    [ $? -eq 0 ] && echo "192.168.51.$i IP is UP!" || echo "192.168.51.$i IP is down!"
done

问题8:函数的使用

#!/bin/bash
#function
filename=""
rename(){
        #$1 $2为函数传递进来的参数,按顺序取值
        filename=$1$2
}
rename hello world
echo -e "filename is $filename"
[ $? == 0 ] && mkdir $filename || echo -e "have error\n"

问题9:select、shift的使用

#!/bin/bash
#select
select flag in 1
do
        case $flag in
                1)echo 1;;
                2)echo 2;;
                3)echo 3;;
                4)echo 4;;
                *)echo "input error"
                break ;;
        esac
done
#shift
echo -e "the all arg is\n"
while [ $1 ]
do
        echo $1
        shift
done

三、补充知识

  1. > 直接把内容生成到指定文件,会覆盖源文件中的内容,还有一种用途是直接生成一个空白文件,相当于touch命令
  2. >> 尾部追加,不会覆盖掉文件中原有的内容
  3. &  表示任务在后台执行,如要在后台运行redis-server,则有  redis-server &
  4. && 表示前一条命令执行成功时,才执行后一条命令 ,如 echo ‘1‘ && echo ‘2’    
  5. | 表示管道,上一条命令的输出,作为下一条命令参数,如 echo ‘yes’ | wc -l
  6. || 表示上一条命令执行失败后,才执行下一条命令,如 cat nofile || echo “fail”
  7. $( )` ` (反引号) 用来命令替换 如 echo $(date) / echo `date`
  8. ${ } 用来作变量替换 ,一般情况下,$var 与 ${var} 并没有啥不一样 
  9.  $(( )) 它是用来作整数运算的,如 $(( a+b*c ))
  10.  `seq 1 10` 生成1,2,3...10的序列

参考博文:https://www.cnblogs.com/handsomecui/p/5869361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值