Shell编程学习笔记

时间:2023-04-27
本文章向大家介绍Shell编程学习笔记,主要内容包括变量、命令替换、重定向输入与输出、输出重定向、输入重定向、数学运算、退出脚本、exit 命令、if-then、使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

变量

设置局部变量

变量名=变量值

设置全局变量

export 变量名=变量值

删除变量

unset 变量名

添加PATH环境变量

PATH=$PATH:[路径]

数组变量

mytest=(one two three four five)

echo $mytest
--->  one

显示数组某个位置的变量
echo ${mytest[2]}
--->  three

显示整个数组变量
echo ${mytest[*]}
--->  one two three four five

命令替换

有两种方法可以将命令输出赋给变量:

1、反引号字符(`)

2、$()格式(推荐)

#!/bin/sh
testing=$(date)
echo "The date and time are: " "$testing"
testing2=`date`
echo "The date and time are: " "$testing2"

重定向输入与输出

输出重定向

最基本的重定向将命令的输出发送到一个文件中。bash shell用大于号(>)来完成这项功能

command > outputfile

如果输出文件已经存在了,重定向操作符会用新的文件数据覆盖已有文件

输入重定向

command < inputfile

数学运算

使用 $((expression))

#!/bin/bash
var1=$((1+5))
echo ${var1}

只支持整数运算,浮点运算需要使用bc,待补充

退出脚本

Linux提供了一个专门的变量$?来保存上个已执行命令的退出状态码。对于需要进行检查的 命令,必须在其运行完毕后立刻查看或使用$?变量。它的值会变成由shell所执行的最后一条命令 的退出状态码。

exit 命令

默认情况下,shell脚本会以脚本中的最后一个命令的退出状态码退出。

可以改变这种默认行为,返回自己的退出状态码。exit命令允许你在脚本结束时指定一 个退出状态码

#!/bin/bash
var1=10
var2=30
var3=$[$var1 + $var2]
echo The answer is $var3
exit 5

if-then

if command 
then 
	commands
fi

if语句会运行if后面的那个命令。如果该命令的退出状态码是0 (该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then 语句到此结束。

if-then-else

if command 
then 
	commands
else commands
fi

当if语句中的命令返回退出状态码0时,then部分中的命令会被执行,这跟普通的if-then 语句一样。当if语句中的命令返回非零退出状态码时,bash shell会执行else部分中的命令。

嵌套 if

if command1
then 
 commands
elif command2
then 
 more commands
fi 

数值比较

value1=10
value2=11

if [ $value1 -gt 5 ]
then
 echo "The test value $value1 is greater than 5"
fi


if [ $value1 -eq $value2 ]
then
 echo "The values are equal"
else
 echo "The values are different"
fi

字符串比较

比较字符串是否相等

#!/bin/bash
testuser=rich
#
if [ $USER = $testuser ]; then
  echo "Welcome $testuser"
else
  echo "who are you"
fi

比较字符串大小

#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
 echo "$val1 is greater than $val2"
else
 echo "$val1 is less than $val2"
fi

判断是否为空

-n和-z可以检查一个变量是否含有数据

#!/bin/bash
# testing string length
val1=testing
val2=''
if [ -n "$val1" ]
then
 echo "The string '$val1' is not empty"
else
 echo "The string '$val1' is empty"
fi
#
if [ -z "$val2" ]
then
 echo "The string '$val2' is empty"
else
 echo "The string '$val2' is not empty"
fi
#
if [ -z "$val3" ]
then
 echo "The string '$val3' is empty"
else
 echo "The string '$val3' is not empty"
fi

文件比较

复合条件测试

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]

使用双括号

双括号命令允许你在比较过程中使用高级数学表达式

#!/bin/bash
val1=10
if (($val1 ** 2 > 90)); then
  ((val2 = $val1 ** 2))
  echo "The square of $val1 is $val2"
fi

使用双方括号

双方括号命令提供了针对字符串比较的高级特性。

#!/bin/bash
if [[ $USER == r* ]]; then
  echo "Hello $USER"
else
  echo "Sorry, I do not know you"
fi

case 命令

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

#!/bin/bash
case $USER in
rich | barbara)
  echo "Welcome, $USER"
  echo "Please enjoy your visit"
  ;;
testing)
  echo "Special testing account"
  ;;
jessica)
  echo "Do not forget to log off when you're done"
  ;;
*)
  echo "Sorry, you are not allowed here"
  ;;
esac

for命令

for var in list 
do 
 commands 
done
#!/bin/bash
for test in Alabama Alaska Arizona Arkansas California Colorado; do
  echo The next state is $test
done

读取复杂的值

添加了反斜线字符来转义don't中的单引号。将this'll用双引号圈起来。

#!/bin/bash
for test in I don\'t know if "this'll" work; do
  echo "word:$test"
done

for命令用空格来划分列表中的每个值。如果在单独的数据值中有 空格,就必须用双引号将这些值圈起来。

#!/bin/bash 
# an example of how to properly define values 
for test in Nevada "New Hampshire" "New Mexico" "New York" 
do 
 echo "Now going to $test" 
done

从变量读取值

#!/bin/bash
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list; do
  echo "Have you ever visited $state?"
done

从命令读取值

#!/bin/bash
file="states"
for state in $(cat $file); do
  echo "Visit beautiful $state"
done

更改字段分隔符

IFS环境变量定义了bash shell用作字段分隔符的一系列字符。默认情况下,bash shell会将下列字 符当作字段分隔符:

空格 制表符 换行符

如果bash shell在数据中看到了这些字符中的任意一个,它就会假定这表明了列表中一个新数据字段的开始。

#!/bin/bash 
file="states" 
IFS=$'\n' 
for state in $(cat $file) 
do 
 echo "Visit beautiful $state" 
done

用通配符读取目录

#!/bin/bash
for file in /home/rich/test/*; do
  if [ -d "$file" ]; then
    echo "$file is a directory"
  elif [ -f "$file" ]; then
    echo "$file is a file"
  fi
done

原文地址:https://www.cnblogs.com/panxianhao/p/17360451.html