linux shell 编程基础知识

$$:获取当前的shell进程号
$?:获取执行上一个指令的返回值(0为成功,非零为失败),可以对上一个命令执行是否成功进行判断。
$_:在此之前执行的命令或脚本的最后一个参数

   $? 变量其实获取的是上一个程序返回给父进程shell的返回值(该值在0-255之间:0表示运行成功,2表示权限拒绝,1~125为运行失败原因是脚本命令、系统命令错误或参数传递错误,126为找到该命令但是无法执行,127为无该命令/程序,>128表示命令被系统强制结束)

Shell if else语句

Shell 有三种 if ... else 语句

  • if ... fi 语句;
  • if ... else ... fi 语句;
  • if ... elif ... else ... fi 语句。
1.
if [ expression ]
then
   Statement(s) to be executed if expression is true
fi

注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误

举个例子:

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
运行结果:

a is not equal to b
2.
if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi

举个例子

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi

执行结果:

a is not equal to b

3.
if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

举个例子:

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi

运行结果:

a is less than b

 

if ... else 语句也可以写成一行,以命令的方式来运行,像这样

  1. if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

 

if ... else 语句也经常与 test 命令结合使用,如下所示:

num1=$[2*3]

num2=$[1+5]

if test $[num1] -eq $[num2]

then

echo 'The two numbers are equal!'

else

echo 'The two numbers are not equal!'

fi

输出:

The two numbers are equal!

test 命令用于检查某个条件是否成立,与方括号([ ])类似

 

awk命令

awk '{pattern + action}' {filenames}

awk工作流程是这样的:读入有'\n'换行符分割的一条记录,然后将记录按指定的域分隔符划分域,填充域,$0则表示所有域,$1表示第一个域,$n表示第n个域。默认域分隔符是"空白键" "[tab]",所以$1表示登录用户,$3表示登录用户ip,以此类推

cat /etc/passwd |awk  -F ':'  '{print $1"\t"$7}'只显示passwd中的第一列和第七列,-F指定域分隔符为':'

ls -l |awk 'BEGIN {size=0;print "[start]size is ", size} {if($5!=4096){size=size+$5;}} END{print "[end]size is ", size/1024/1024,"M"}'

统计某个文件夹下的文件占用的字节数,过滤4096大小的文件(一般都是文件夹):

linux暂停一个在运行中的进程

kill –STOP,暂停进程
kill –CONT,恢复进程

dumpsys meminfo:查看RAM使用情况

df:查看ROM挂载情况

dumpsys audio 主要包含每个stream volume的信息

adb logcat -v time 查看Android中的log

在同时输出到屏幕和文件 tee

想要把日志保存到文件,如果采用IO重定向,就无法输出到屏幕, 针对这个问题可以采用 tee命令

adb logcat | grep -E '^[VDE]/(TAG1|TAG2)' | tee my.log

查看相关寄存器

lookat -l num addr

猜你喜欢

转载自blog.csdn.net/qq_33600019/article/details/82841481