Shell语法

本文详细介绍了Shell的基础语法,包括bash shell、环境变量、数值运算、控制结构(如if嵌套、for循环、while循环)以及函数。还提供了多个实例,如猜密码游戏、检查目录下文件存在和时间问候等,帮助读者深入理解和应用Shell脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shell语法

关键词: shell shell脚本

主要内容:

  • shell 基础语法
  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪贪贪丶慎独

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值