shell编程基础2

一、shell脚本传参

4.1.shell文件中输入

echo $1 $2 $3

su test.sh one two three
打印出对应输入的$1 $2 $3参数one two three
也可以使用args数组方式存储参数

args=("$@")
echo ${args[0]} ${args[1]} ${args[2]}

还有简洁的方式

args=("$@")
#echo ${args[0]} ${args[1]} ${args[2]}
echo $@

二、if-else语句

在shell中的if控制语句很像python中的语

  1. if -then
if condition
then
    command1
    command2
    ...
    commandN
fi

2.if--else语句

if condition
then
    command1
    command2
    ...
    commandN
else
    command
fi

例如:

echo -e "Enter the name of file: \c"
read filename

if [ -e $filename ]#检查文件是否存在
then
  echo "File found"
else 
  echo "File is not exist or not found"
fi

echo -e "Enter the name of file: \c"
read filename

if [ -f $filename ]#检查文件是常规文件
then
  echo "File found"
else 
  echo "File is not exist or not found"
fi

echo -e "Enter the name of file: \c"
read filename

if [ -d $filename ]#-d表示directory,判断是否是一个目录
then
  echo "File found"
else 
  echo "File is not exist or not found"
fi

注意 -e表示是否存在
-f表示file,判断是否是常规的文件
-d表示directory,判断是否是一个目录。
-e表示empty,默认文件是不为空。
3.if-then-elif-then-else

if condition1
then
    command1
elif condition2
then
    command2
else
    commandN
fi

例如:

a=10
b=20
if [ $a == $b ]
then
   echo "a == b"
elif [ $a -gt $b ]
then
   echo "a > b"
elif [ $a -lt $b ]
then
   echo "a < b"
else
   echo "Ineligible"
fi

4.利用if在文件尾部写入内容
首先要判断是不是常规文件,如果是,在判断是否有写的权限,如果有写得权限需要输入,并不输入的内容保持到文件的维度
上面可以看出这里需要嵌套if,
文件写入需要用到cat命令 cat > file 就是覆盖 cat >> file就是不覆盖,直接在后面写入新内容

echo -e "echo -e "Enter the name of file: \c"
read filename

if [ -f $filename ]#判断文件是否存在
then
  if [-w $filename ]#判断写权限
   then
    echo "type some test date. press ctrl+d to quit."
    cat >> $filename
  else
    echo "$filename do not have write permissiong"
  fi
else
  echo "$filename is not exit"
fi

2.for循环
循环一般格式为:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
dovimne

例如:

for str in This is a string
do
    echo $str
done

还有一种
for (( 表达式1; 表达式2;表达式3))
do
command1
command2
command3
done
例如

t=0
for ((i=1; i<=100; i++ ))
do
  t=$(( t +i ))
done
echo $t

使用sh filename.tes这里会发现Syntax error: Bad for loop variable
因为sh 命令执行脚本的时候实际使用的是 dash,而 dash 不支持这种 C 语言格式的 for 循环写法。
所以,使用 bash 代替 sh 运行脚本 bash filename.tes

3.while语句
1.常见的while语句格式

while condition
do
    command
done

例如

#!/bin/bash
int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

无线循环

while :
do
    command
done
或者
while true
do
    command
done
或者前面学到的for
for (( ; ; ))

4.until循环,until 循环执行一系列命令直至条件为真时停止
格式

until condition
do
    command
done

例如:打印1到9

n=1
until [ $n -ge 10 ]
do
  echo "$n"
  (( n++ ))
done

5.case
case 语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case 语句格式如:

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac
注意:取值后面必须为单词 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

例如:

echo 'Enter a number between 1 and 4:'
echo 'The number you entered is:'
read aNum
case $aNum in
    1)  echo 'You have chosen 1'
    ;;
    2)  echo 'You have chosen 2'
    ;;
    3)  echo 'You have chosen 3'
    ;;
    4)  echo 'You have chosen 4'
    ;;
    *)  echo 'You did not enter a number between 1 and 4'
    ;;
esac

6.跳槽循环
break 和 continue
break 命令允许跳出所有循环(终止执行后面的所有循环)
例如

while :
do
    echo -n "Enter a number between 1 and 5:"
    read aNum
    case $aNum in
        1|2|3|4|5) echo "The number you entered is $aNum!"
        ;;
        *) echo "The number you entered is not between 1 and 5! game over!"
            break
        ;;
    esac
done

continue 命令与 break 命令类似,只有一点差别,它不会跳出所有循环,只跳出当前循环。

while :
do
    echo -n "Enter a number between 1 and 5: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "The number you entered is $aNum!"
        ;;
        *) echo "The number you entered is not between 1 and 5!"
            continue
            echo "game over"
        ;;
    esac
done

三、数组

arr=('Alice' 'Jone' 'Jack' 'Lili')
arr[4]='Alex'#添加元素

echo "${arr[@]}"#遍历数组
echo "${arr[1]}"#单个制定位置的元素
echo "${!arr[@]}"#数组的索引查询
echo "${#arr[@]}"#数组的长度
unset  arr[1]# 删除数组

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. shell脚本: 包含一些命令或声明,并符合一定格式的文本文件  格式要求:首行shebang(#!)机制...
    尛尛大尹阅读 1,994评论 0 1
  • Bash内置基本变量 PWD : 显示当前的工作目录 OLDPWD : 显示上一次的工作目录 ~:用户家目录 - ...
    魏镇坪阅读 1,652评论 0 6
  • 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系...
    呼啦啦的爱阅读 269评论 0 1
  • 什么是Shell Shell是用户与内核进行交互操作的一种接口,目前最流行的Shell称为bash Shell S...
    花丶小伟阅读 308评论 0 0
  • 程序:什么是程序 程序是指令加数据来组合来完成 真正关心的是对数据的处理,通过指令对数据的操作 程序编程风格 在程...
    数据革命阅读 490评论 0 0

友情链接更多精彩内容