Linux Bash脚本编写

read命令

read命令的使用

脚本参数传递

算术运算

反引号 ` ,位于键盘~键中

expr与变量之间要有间隔

乘号 * ,需要用 \ 反斜杠 编译

算术运算3

bash与内存

bash与linux命令的混合使用,bash与目录命令

默认变量

设置默认变量

read  回车

输入:123  回车

读取默认变量

echo $REPLY

设置普通变量

read a b c 回车

输入3个参数  10 20 30  回车

读取普通变量

echo $a $b $c

编写test.sh脚本文件

vi  test.sh

按i键插入内容

输入以下内容

#!/bin/bash
echo $1,$2,$3
echo "filename:"$0
echo "参数数量:"$#
echo "all: "$*
echo "return:"$?
 

输入完成保存退出,按esc键,:wq

运行test.sh脚本文件

编写bash脚本counter.sh内进行算术运算符运算,并打印

#!/bin/bash
a=10
b=20
echo `expr $a + $b`
echo `expr $a - $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b` 

运行算术运算符运算脚本

bash counter.sh

编写bash脚本mix.sh,与linux命令混合使用

!/bin/bash
echo "create directory test"
mkdir test
cd test
ls
echo "save content to file test.txt"
echo "hello" > test.txt
cat test.txt
echo "contant directory"
pwd
echo "remove  all file or directory "
rm -r *
ls
echo "quit from test"
cd ..
pwd
ls
echo "remove directory test"
rm -r test
ls
​​​​​​​

bash脚本内使用if判断语句

#!/bin/bash
if [ $1 == $2 ]
then
        echo "equal"
elif [ $1 -gt $2 ]
then
        echo "bigger "
elif [ $1 -lt $2 ]
then
        echo "smaller"
elif [ $1 -ge $2 ]
then
        echo "bigger or equal"
elif [ $1 -le $2 ]
then
        echo "smaller or equal"
fi
运行脚本

bash if_test.sh 10 20

bash if_test.sh 20 10

bash if_test.sh 10 10

ps aux

ps aux | awk '{print $3}'

ps aux | awk '{print $3}' | grep -v PGID

ps aux | awk '{print $3}' | grep -v %CPU
 

​​​​​​​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值