Linux系统之 Shell 脚本

Shell 脚本并不能作为正式的编程语言,因为它是在Linux的Shell 中运行的,所以称他为Shell 脚本。
 一个 Shell 脚本是一个文本文件,包含一个或多个命令。
 
作为系统管理员,我们经常需要使用多个命令来完成一项任务,我们可以添加这些所有命令在一个文本文件(Shell 脚本)来完成这些日常工作任务。

if 语法如何嵌套?

if [ 条件 ]
then
命令1
命令2..
else
if [ 条件 ]
then
命令1
命令2.
else
命令1
命令2..
fi
fi

在 Shell 脚本中如何比较两个数字?
关系运算符:

-eq:检测两个数是否相等,相等返回 true。
-ne:检测两个数是否不相等,相等返回 true。
-gt:检测左边的数是否大于右边的,如果是返回 true。
-lt:检测左边的数是否小于右边的,如果是返回 true。
-ge:检测左边的数是否大于等于右边的,如果是返回 true。
-le:检测左边的数是否小于等于右边的,如果是返回 true。

在 if-then 中使用测试命令( -gt 等)来比较两个数字。

#!/bin/bash

a=11
b=22
if [ $a -gt $b ]
then
   echo "a is greater than b"
else
   echo "b is greater than a"
fi
cxx@ubuntu16:~/my_shell$ ./shell_gt.sh 
b is great than a
cxx@ubuntu16:~/my_shell$ 

Shell 脚本基本的读取

read 命令可以读取来自终端(使用键盘)的数据。
read 命令得到用户的输入并置于你给出的变量中。
命令选项:
		-p:指定读取值时的提示符;
		-t:指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了。
		-n:设置允许输入字符的个数
#!/bin/bash
# testing the read option

read -p "Please enter your name : " name
echo "Hello $name."
cxx@ubuntu16:~/my_shell$ ./shell_read.sh 
Please enter your name : chenxx
Hello chenxx.
cxx@ubuntu16:~/my_shell$ 

Shell 脚本中 for 循环语法?
基础语法:

for 变量 in 循环列表
do
命令1
命令2
….
最后命令
done

如何执行算术运算?有两种方法来执行算术运算:

1、使用 expr 命令:## expr 5 + 2 
2、用一个美元符号和方括号($[ 表达式 ]):test=$[16 + 4] ; test=$[16 + 4] 

Shell 脚本中 while 循环语法?
如同 for 循环,while 循环只要条件成立就重复它的命令块。
不同于 for循环,while 循环会不断迭代,直到它的条件不为真。
基础语法:

while [ 条件 ]
do
命令…
done

do-while 语句的基本格式?
do-while 语句类似于 while 语句,但检查条件语句之前先执行命令,至少执行一次。下面是用 do-while 语句的语法:

do
{
命令
} while (条件)

    1
    2
    3
    4

test.sh

#!/bin/bash
while true
do
        read -p "please input a positive number: " num
        # 判断数是否是整数
        expr $num + 1 &> /dev/null
        if [ $? -eq 0 ];then
                # 判断这个整数是否大于0,大于0返回1
                if [ `expr $num \> 0` -eq 1 ];then
                        #echo "yes,positive number"
                        # $sum没有赋值,默认为0
                        for i in `seq 0 $num`
                        do
                                sum=`expr $sum + $i`
                        done
                        echo "1+2+3+...+$num = $sum"
                        # 执行计算需要退出
                        exit
                fi
        fi
        echo "error,input enlegal"
        continue
done
cxx@ubuntu16:~/my_shell$ ./test.sh
please input a positive number: 100
1+2+3+...+100 = 5050
cxx@ubuntu16:~/my_shell$ 

Shell 脚本中 break 命令的作用?

break 命令一个简单的用途是退出执行中的循环。我们可以在 while 和 until 循环中使用 break 命令跳出循环。

Shell 脚本中 continue 命令的作用?
continue 命令不同于 break 命令,它只跳出当前循环的迭代,而不是整个循环。continue 命令很多时候是很有用的,例如错误发生,但我们依然希望继续执行大循环的时候。

Shell 脚本中 case 语句的语法?

基础语法如下:

case 变量 in
值1)
命令1
命令2
…..
最后命令
!!
值2)
命令1
命令2
……
最后命令
;;
esac

在 Shell 脚本如何定义函数呢?

函数是拥有名字的代码块。当我们定义代码块,我们就可以在我们的脚本调用函数名字,该块就会被执行。示例如下所示

$ diskusage () { df -h ; }
译注:下面是我给的shell函数语法,原文没有
[ function ] 函数名 [()]
{
命令;
[return int;]
}

提取路径的目录和文件名

# 提取目录:
dirname $path
# 提取文件名:
basename $path

批量重命名带有空格文件

function processFilePathWithSpace(){ 
 find $1 -name "* *" | while read line
 do 
 newFile=`echo $line | sed 's/[ ][ ]*/_/g'`
 mv "$line" $newFile
logInfo "mv $line $newFile $?"
 done
} 

遍历文件内容

cat /tmp/text.txt | while read line
do
 echo $line
done

文件不存在,则创建文件

[ -f $logFile ] || touch $logFile

递归遍历目录

function getFile(){
 for file in `ls $1`
 do
  element=$1"/"$file
     if [ -d $element ]
  then 
         getFile $element
     else
         echo $element
     fi  
 done
}

清空文件内容

cat /dev/null > $filePath

判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下?

#!/bin/bash
read -p "Input file name: " FILENAME
if [ -c "$FILENAME" ];then
  cp $FILENAME /dev
fi

添加一个新组为 class1 ,然后添加属于这个组的 30 个用户,用户名的形式为 stdxx ,其中 xx 从 01 到 30 ?

#!/bin/bash
groupadd class1
for((i=1;i<31;i++))
do
        if [ $i -le 10 ];then
                useradd -g class1 std0$i
        else
                useradd -g class1 std$i
        fi
done

编写 Shell 程序,实现自动删除 50 个账号的功能,账号名为stud1 至 stud50 ?

#!/bin/bash
for((i=1;i<51;i++))
do
                userdel -r stud$i
done
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程一时爽Cxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值