linux shell脚本之-变量极速入门与进阶(2)

1,$$:显示当前的进程id号

ghostwu@dev:~/linux/shell/how_to_use_var$ cat show_pid.sh 
#!/bin/bash
echo $$
sleep 3000
ghostwu@dev:~/linux/shell/how_to_use_var$ bash show_pid.sh &
[1] 9401
ghostwu@dev:~/linux/shell/how_to_use_var$ 9401

ghostwu@dev:~/linux/shell/how_to_use_var$ ps -ef | grep show_pid
ghostwu   9401  2792  0 06:07 pts/18   00:00:00 bash show_pid.sh
ghostwu   9404  2792  0 06:07 pts/18   00:00:00 grep --color=auto show_pid
ghostwu@dev:~/linux/shell/how_to_use_var$ ps -ef | grep show_pid | grep -v grep
ghostwu   9401  2792  0 06:07 pts/18   00:00:00 bash show_pid.sh
ghostwu@dev:~/linux/shell/how_to_use_var$ kill 9401
ghostwu@dev:~/linux/shell/how_to_use_var$ ps -ef | grep show_pid
ghostwu   9478  2792  0 06:08 pts/18   00:00:00 grep --color=auto show_pid
[1]+  Terminated              bash show_pid.sh

 2,(())用于整数的常用运算符

>把两个整数的运算结果赋值给一个变量,前面要加$

ghostwu@dev:~/linux/shell/how_to_use_var$ a=((10+20))
bash: syntax error near unexpected token `('
ghostwu@dev:~/linux/shell/how_to_use_var$ a=$((10+20))
ghostwu@dev:~/linux/shell/how_to_use_var$ echo $a
30

3,四则运算

ghostwu@dev:~/linux/shell/how_to_use_var$ bash calc.sh 10 2
a+b=12
a-b=12
a*b=12
a/b=12
a**b=100
a%b=0
ghostwu@dev:~/linux/shell/how_to_use_var$ cat calc.sh 
#!/bin/bash
a=$1
b=$2
echo "a+b=$(($a+$b))"
echo "a-b=$(($a+$b))"
echo "a*b=$(($a+$b))"
echo "a/b=$(($a+$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"

4,let用于整数运算,类似(())

ghostwu@dev:~/linux/shell/how_to_use_var$ i=10
ghostwu@dev:~/linux/shell/how_to_use_var$ let i=i+10
ghostwu@dev:~/linux/shell/how_to_use_var$ echo $i
20

不使用let,是不会计算变量的值

ghostwu@dev:~/linux/shell/how_to_use_var$ i=2
ghostwu@dev:~/linux/shell/how_to_use_var$ i=i+8
ghostwu@dev:~/linux/shell/how_to_use_var$ echo $i
i+8

  5,bash内置命令read,通过参数-p 提示信息,读入变量的值

ghostwu@dev:~/linux/std_err_out$ read -p "pls input 2 number:" a b
pls input 2 number:10 20
ghostwu@dev:~/linux/std_err_out$ echo $a $b
10 20

10,test -f 判断普通文件是否存在

ghostwu@dev:~/linux/std_err_out$ ls
ghostwu.txt  output_error.txt  std_out1.txt  std_out.txt
ghostwu@dev:~/linux/std_err_out$ test -f ghostwu.txt && echo 1 || echo 0
1
ghostwu@dev:~/linux/std_err_out$ test -f ghostwu2.txt && echo 1 || echo 0
0

test -z 测试字符串长度是否为0

ghostwu@dev:~/linux/std_err_out$ test -z "hello" && echo 1 || echo 0
0
ghostwu@dev:~/linux/std_err_out$ test -z "" && echo 1 || echo 0
1

中括号[]与test一样.

ghostwu@dev:~/linux/std_err_out$ [ -f ghostwu.txt ] && echo 1 || echo 0
1
ghostwu@dev:~/linux/std_err_out$ [ -f ghostwu2.txt ] && echo 1 || echo 0
0

11,判断一个变量值或者字符串是否为整数?

利用expr做计算时变量或者字符串必须是整数的规则,把一个变量或字符串和一个已知的整数(非0)相加,看命令返回的值是否为0。如果为0,就认为做加法的变量或字符串为整数,否则不是整数。

ghostwu@dev:~/linux/shell/flow_control$ i=10
ghostwu@dev:~/linux/shell/flow_control$ expr $i + 1 >/dev/null
ghostwu@dev:~/linux/shell/flow_control$ echo $?
0
ghostwu@dev:~/linux/shell/flow_control$ i='a'
ghostwu@dev:~/linux/shell/flow_control$ expr $i + 1 >/dev/null 2>&1
ghostwu@dev:~/linux/shell/flow_control$ echo $?
2

 

posted @ 2018-05-30 22:30 ghostwu 阅读(...) 评论(...) 编辑 收藏
Copyright ©2017 ghostwu