shell学习1

文章介绍了Shell脚本的基础知识,包括#!/bin/bash的首行声明,使用$()和反引号来输出变量,重定向(>和>>)操作,以及数学命令expr和方括号[]。接着讨论了if-then语句和其扩展形式,包括else分支和嵌套if。最后提到了case命令用于文件选项的比较和处理。
摘要由CSDN通过智能技术生成

shell脚本的首行

#!/bin/bash

输出变量

反引号(`)

·$()格式

重定向与追加

重定向:(>)

data>文件.txt    将data添加到文件中,原文件内容替换成data

追加:(>>) 

data>>文件.txt    将data添加到文件中,原文件内容保留,多了data内容

数学命令(expr与方括号[])

这两个命令只支持整数运算

#!/bin/bash

var1=2
var2=3
var3=5
var6=$[var1 + var2]
var7=$[var2*var3]
echo $var6
echo $var7

输出:
5
15

 查看退出状态码

echo $0

 if-then

if command

then

commands

fi

if-then-else

if command
then
   commands
else
   commands
fi

嵌套if语句

if command1
then
   commands
elif command2
then
    more commands
fi

 文件比较

case命令

case命令会将指定变量与不同模式进行比较。如果变量与模式匹配,那么shell就会执行为该模式指定的命令。你可以通过竖线运算符在一行中分隔出多个模式。星号会捕获所有与已知模式不匹配的值

$ cat extractoptions.sh
#!/bin/bash
# Extract command-line options
#
echo
while [ -n "$1" ]
do
     case "$1" in
          -a) echo "Found the -a option" ;;
          -b) echo "Found the -b option" ;;
          -c) echo "Found the -c option" ;;
          *) echo "$1 is not an option" ;;
     esac
     shift
done
echo
exit
$
$ ./extractoptions.sh -a -b -c -d

Found the -a option
Found the -b option
Found the -c option
-d is not an option

$

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值