Shell语法
关键词: shell shell脚本
主要内容:
- shell 基础语法
- shell 脚本实例
文档参考:
- https://www.cnblogs.com/klb561/p/8933945.html Linux c 执行shell语句
- http://www.runoob.com/linux/linux-shell.html shell编程菜鸟教程
最新地址: https://taotaodiy-linux.readthedocs.io/en/latest/linux/shell/shell.html
bash shell
命令记忆功能 命令和文件补全 命令别名设置 作业控制,前台、后台控制 通配符
环境变量和用户定义变量
- $HOME 当前用户主目录
- $PATH 以冒号分隔的用来搜索命令的目录列表
- $PS1 命令提示符
- $PS2 二级提示符
- $IFS 输入域分隔符
- $LANG 语系
- $HISTSIZE 历史记录长度
- $SHELL
- $OSTYPE $HOSTYPE $MACHTYPE 主机硬件与内核等级
声明变量 --a数组 --f函数 --i整数 --r只读变量 --x全局变量
命令执行的判断依据 ; , && || $?命令回传码
shell脚本
#!/bin/sh
#show a directory
echo "input directory: "
read DIRECTORY
cd $DIRECTORY
ls -al
shell脚本的位置参数
- $0 shell脚本的文件名字
- $1-9命令行参数1-9
- $*所有参数 $@
- $#命令行参数的总个数
- $?最近一次命令的退出状态
- $!最近一次后台后台进程ID号
内部参数
- $1-9为第1到9个参数
- $0表示命令本身
- $#传递给程序的总的参数数目
- $?shell程序在shell中的退出情况,正常返回0,反之非0
- $*传递给程序的所有参数组成的字符串
Shell环境变量 Bash环境变量 /etc/profile Shell元字符
变量引用 本质:字符串替换
新建hello shell脚本
vim hello.sh
编辑脚本
example hello shell
#!/bin/bash
echo hello shell
运行脚本 方式1
sh hello.sh
运行脚本 方式2
chmod +x hello.sh
./hello.sh
变量
普通变量
#!/bin/bash
#variable
hello="hello shell"
echo $hello
数组
#!/bin/sh
#array
array[0]=1
array[1]=2
array[3]=3
temp=(0 1 2 3 4 5 6)
buffer=( [0]=0 [1]=5 [5]=10 )
echo ${array[1]}
echo ${temp[@]:2:5}
echo ${buffer[@]:0:2}
array_lenth=${#array}
temp_lenth=${#array[0]}
echo $array_lenth $temp_lenth
unset temp[4]#
echo ${temp[@]:0}
array_lenth=${#array[@]}
temp_lenth=${#temp[*]}
echo $array_lenth
echo $temp_lenth
#!/bin/sh
#sum the value in array
array=(1 3 5 7 9 2 6 5 3 5)
count=${#array[@]}
sum=0
for ((index=0;index<$count;index++))
do
value=${array[$index]}
let sum=$sum+$value
done
echo $sum
数值运算
let expr $((expression))
#!/bin/sh
#numerical processing
let "x=5" "y=5" "z=10"
let a=x+y
echo $a
let b=x*x c=x*y d=x**z
echo $b $c $d
e=$((x/x))
f=$(($z/$y))
g=$(((20*1+1+1)*2/10))
echo $e $f $g
h=$(expr $x + 5)
i=$(expr x+5)
echo $h
echo $i
#!/bin/sh
#count file in directory
echo "input your test directory: "
read input
count=0
for number in `ls $input`
do
echo $number
let count=$count+1
done
echo "count: $count"
结构控制语句
if嵌套
#!/bin/sh
#delete your file
if test $# -eq 0
then
echo "Please input your file name"
else
echo -n "Are you sure delete $1 y/n"
read CHOSE
if test $CHOSE = 'y'
then
rm -rf $1
echo "$1 deleted"
elif test $CHOSE = 'n'
then
echo "you cancle delete $1"
else
echo "input error"
fi
fi
#!/bin/sh
#browse your file or directory
echo "input your file or directory"
read input
if [ -d $input ]
then
ls $input
elif [ -f $input ]
then
cat $input
else
echo "input error"
fi
#!/bin/sh
#use vim creator file
echo "input creator file name"
read input
#cd /usr/bin
if [ -f /usr/bin/vim ]
then
vim ./$input
else
echo "no vim"
fi
for循环
#!/bin/sh
#sum 1+1/2+1/3+...+1/n
echo "input the value of n"
read n
sum=0.000
count=0.000
for((num=1;num<=$n;num++));do
i=$num
if [ $i != 0 ]
then
count=`echo "scale=3;1.000/$i" | bc`
sum=`echo "scale=3;$sum+$count" | bc`
fi
done
echo "sum: $sum"
while循环
#!/bin/sh
#1+2+..+100
x=1
sum=0
while [ $x -le 100 ]
do
let sum=$sum+$x
let x=$x+1
done
echo $sum $x
while [ $x -le 100 ]
do
sum=$(($sum+$x))
x=$(($x+1))
done
echo $sum $x
while [ $x -le 100 ]
do
sum=$( expr $sum + $x )
x=$( expr $x + 1 )
done
echo $sum $x
switch…case
>#!/bin/sh
#shoe menu
while true
echo " Menu"
echo " (1) show a directory"
echo " (2) delete your file"
echo " (3) browse your file or directory"
echo " (4) use vim creator file"
echo " (5) count file in directory"
echo " (6) sum 1+1/2+1/3+...+1/n"
echo " (7) guess password"
echo " (8) simple calculator V0.1"
echo " (9) Exit menu"
echo -n " select: "
read input
if [ $input = 9 ]
then
exit 0
fi
do
case $input in
1) sh shell_1;;
2) sh shell_2;;
3) sh shell_3;;
4) sh shell_4;;
5) sh shell_5;;
6) sh shell_6;;
7) sh shell_7;;
8) sh shell_8;;
*) echo "select 1\2\3\4\5\6\7\8\9";;
esac
done
函数
#!/bin/bash
#函数定义
function func ( )
{
#echo "$1 $2"
A=`expr $1 + $2`
echo $A
exit 1
}
#函数的调用
#func 10 20
B=`func 10 20`
echo $B
exit
实例
猜密码
#!/bin/sh
#guess password
echo "Guess the password, exit over game"
echo -n "enter your guess password:"
read input
while [ "$input" != "i996izioq." ]
do
if [ "$input" = "exit" ]
then
exit 0
else
echo "sorry, try again"
echo -n "enter your guess password:"
read input
fi
done
echo "oh, you are genius"
判断某个目录下是否存在某个文件
#!/bin/bash
#Determine whether a file exists in a directory
#$1 User directory
#$2 test file
#判断参数
if [ $# -lt 2 ]
then
echo "参数:目录+测试文件"
exit
fi
#测试目录是否合法
if [ ! -d "$1" ];then
echo "目录有误"
exit -1
fi
#测试文件是否为空
if [ ! $2 ]; then
echo "文件不能为空"
exit -1
fi
#判断文件是否在该文件夹下存在
if [ -f "$1$2" ];then
echo "文件存在"
else
echo "文件不存在"
fi
根据系统当前的时间向用户输出问候信息
#!/bin/bash
#获取当前时间,问候调用者
hour=`date +%H`
echo $hour
if [ "$hour" -lt 11 ];then
echo "早上好"
elif [ "$hour" -ge 11 ]&&[ "$hour" -lt 14 ];then
echo "中午好"
elif [ "$hour" -ge 14 ]&&[ "$hour" -lt 19 ];then
echo "下午好"
else
echo "晚上好"
fi
简易计算器
#!/bin/sh
#simple calculator V0.1
#2019.1.3 bluseli
echo "Thanks for using"
echo -n "input a number: "
read result
while :
do
echo -n "input operator +-*/, exit over:"
read operator
if [ "$operator" = "exit" ]
then
exit 0
fi
echo -n "input next numebr: "
read number
if [ "$operator" = "+" ]
then
let result=$result+$number
elif [ "$operator" = "-" ]
then
let result=$result-$number
elif [ "$operator" = "*" ]
then
let result=$result*$number
elif [ "$operator" = "/" ]
then
let result=$result/$number
else
echo "oprator error"
fi
echo "result: $result"
echo -n "keep result?y/n: "
read keep
if [ "$keep" = "y" ]
then
result=$result
elif [ "$keep" = "n" ]
then
result=0
echo -n "input a number: "
read result
else
echo "error input, default keep result"
fi
done