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