linux Bash编程基本语法

定义和使用变量

var="123"

echo $var

readonly var -->只读变量

unset var -->删除变量

拼接字符串 greeting="hello,"$yourname"!"

数组 array_name=(value0 value1 value2 value3)

1.取数组 valuen=$(array_name[n])

2.单独赋值 array_name[0]=value0

 

实战

创建数组

a=(a b c)

读取值

echo ${a[0]}
-->a
manshuo@Dashuo:~$ echo ${a[1]}
-->b
manshuo@Dashuo:~$ echo ${a[2]}
-->c
manshuo@Dashuo:~$ echo ${a[3]}
manshuo@Dashuo:~$ echo ${a[*]}
-->a b c

if condition ;
then

  command 1;

fi

 

eg:

比较a b值大小:

if [ $a -eq $b ];then echo "equal";elif [ $a -gt $b ]; then echo "a>b";elif [ $a -lt $b ]; then echo "a<b"; fi

#空格必须有,不然会报错

for

for var in item1 item2 ..

do

  command1

  command2

done

eg:

for loop in 1 2 3 4

do

  echo "hello"

done

打印所有1.txt的内容

for i in $(cat 1.txt);

do

  echo $i;

done

 

 while condition

do

  command

done

eg:

int=1

while (( $int<=5 ))

do

  echo $int

  let "int++"

done

循环读取文件内容并输出

while read line;do echo $line;done<dir.txt

posted @ 2022-04-13 15:49  lms21  阅读(38)  评论(0)    收藏  举报