shell 脚本基础学习

shell脚本

简单了解来说就是 将多条命令放到一个文件里,扩展名为 .sh

运行第一个shell脚本

#!bin/bash
echo "hello,world"

交互式shell脚本

#!/bin/bash
echo "please input name:"

read name
echo "your name :" $name
#!/bin/bash
read -p "Please input your age and height :" age height
echo "your age = $age , your height = $height "

shell脚本的数值计算

#!/bin/bash
echo "Please input two number"
read -p "Please input the first num" first
read -p "Please input the second num" second

total=$(($first+$second)) //一定要注意这里 total 后面不要有空格,会报错

echo $total

shell脚本的test

#!/bin/bash
echo "Please input two number"
read -p "Please input the first filename" file1
test -e $file1 && echo "$file1 is exist" || echo "$file1 is not exist"

注意这里的 && 和 || 用法不是与和或,这里是 这个文件存在,输出文件存在;若这个文件不存在,输出文件不存在


shell脚本的参数列表

!/bin/bash
echo "file name:" $0 //表示当前shell文件就是 $0
echo "total canshu:" $# //表示总的参数
echo "total context:" $@ //表示全部内容
echo "first canshu:" $1 //表示第一个参数
echo "second canshu" $2 // 表示第二个参数

shell 脚本的条件判断

#!/bin/bash
read -p "Please input(Y/N)" value

if [ "$value" == "Y" ] ||  [ "$value" == "y" ]; then
        echo "You are sucessful"
        exit 0
fi

if [ "$value" == "N" ] || [ "$value" == "n" ]; then
        echo "You are failure!"
        exit 0

fi

用case的方法

!/bin/bash
case $1 in
        "a")
           echo "The text is:a"
           ;;
        "b")

           echo "The text is:b"
           ;;
         "c")
           echo "The text is:c"
           ;;
esac

shell传参数

#!/bin/bash
print()
{
   echo "canshu1:" $1
   echo "canshu2:" $2

}

print a b //注意这里直接写就好了

shell 循环

while循环写法

#!/bin/bash
while [ "$value" != "c" ]
do
     read -p "Please input the thing:" value

done
echo "STOP!"

for循环写法

#!/bin/bash
read -p "Please input a number to arrive destination :" num
sum=0
for((i=1;i<=num;i++))
do
        sum=$(($sum+$i))
done

echo $sum
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值