linux shell 脚本攻略学习1

时间:2021-12-07 15:38:29

1.关于echo和printf打印输出

 

如果要使用转义序列,那么需要在echo 后面加上参数 -e来进行识别,下面例子是进行对比:

amosli@amosli-pc:~/learn$ echo -e "1\t2\t3\t"
1    2    3    
amosli@amosli-pc:~/learn$ echo "1\t2\t\3\t"
1\t2\t\3\t

输出彩色输出:

颜色码:重置为0,黑色为30,红色为31,绿色为32,黄色为33,蓝色为34,洋红为35,青色为36,白色为37.

echo -e "\e[31m this is red text" //将终端输出颜色变为红色
echo -e "\e[0m" //重置终端颜色
echo -e "\e[35m" //洋红,其他与此类似

2.查看程序进程

amosli@amosli-pc:~$ pgrep gedit  //pgrep 进程名
4681      //输出进程ID 

查年环境变量位置:

格式:

cat /proc/$PID/environ

实例:

amosli@amosli-pc:~$ cat /proc/4681/environ
GNOME_KEYRING_PID=1892USER=amosliLANGUAGE=enLC_TIME=zh_CN.UTF-8COMPIZ_CONFIG_PROFILE=ubuntuHOME=/home/amosliDESKTOP_SESSION=ubuntuXDG_SESSION_COOKIE=cb0be16eb0e8024c05540d520000000c-1387289821.329921-1960133517XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0LC_MONETARY=zh_CN.UTF-8GNOME_KEYRING_CONTROL=/tmp/keyring-T9xO1vUBUNTU_MENUPROXY=libappmenu.soMANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.pathLOGNAME=amosliGTK_IM_MODULE=ibusDEFAULTS_PATH=/usr/share/gconf/ubuntu.default.pathPATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamesXDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0LC_ADDRESS=zh_CN.UTF-8DISPLAY=:0.0LANG=en_US.UTF-8LC_TELEPHONE=zh_CN.UTF-8XAUTHORITY=/home/amosli/.XauthorityXMODIFIERS=@im=ibusSHELL=/bin/bashLC_NAME=zh_CN.UTF-8GDMSESSION=ubuntuLC_MEASUREMENT=zh_CN.UTF-8LC_IDENTIFICATION=zh_CN.UTF-8QT_IM_MODULE=ibusPWD=/home/amosliXDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdgLC_NUMERIC=zh_CN.UTF-8LC_PAPER=zh_CN.UTF-8SSH_AUTH_SOCK=/tmp/keyring-T9xO1v/sshSSH_AGENT_PID=1940DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-78xnDYcrG7,guid=aeebe29452c0ff84e71d27ab0000006eGNOME_DESKTOP_SESSION_ID=this-is-deprecatedSESSION_MANAGER=local/amosli-pc:@/tmp/.ICE-unix/1903,unix/amosli-pc:/tmp/.ICE-unix/1903XDG_CURRENT_DESKTOP=UnityGPG_AGENT_INFO=/tmp/keyring-T9xO1v/gpg:0:1GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/gedit.desktopGIO_LAUNCHED_DESKTOP_FILE_PID=4681DESKTOP_STARTUP_ID=nautilus-1994-amosli-pc-gedit-1_TIME7717031amosli@amosli-pc:~$ 

3.变量的定义方式以及输出方式:

amosli@amosli-pc:~$ var=value //对变量赋值 var = value 是错误的 也可是var = "value"

amosli@amosli-pc:~$ echo $var //输出方式1
value
amosli@amosli-pc:~$ echo ${var}//输出方式2
value

实例:variables.sh 

fruit=apple
count=5
echo "We have $count ${fruit}(s)"
amosli@amosli-pc:~/learn$ sh variables.sh 
We have 5 apple(s)

echo $变量名 即可输出所对应的变量值,如常用的,echo $(JAVA_HOME,HOME,PWD,UID,USER,SHELL,PATH)等等

length={#var} 长度

amosli@amosli-pc:~/learn$ echo ${HOME}
/home/amosli
amosli@amosli-pc:~/learn$ echo ${#HOME} //查看长度,只需要在变量名前加上一个#即可
12

 

另外:echo $SHELL =echo $0都可获得shell的版本.

 

 4.算术操作

amosli@amosli-pc:~/learn$ no1=4;
amosli@amosli-pc:~/learn$ no2=5;
amosli@amosli-pc:~/learn$ let result=no1+no2;
amosli@amosli-pc:~/learn$ echo $result
9

 

amosli@amosli-pc:~/learn$ let no1++
amosli@amosli-pc:~/learn$ echo $no1
5
amosli@amosli-pc:~/learn$ let no2--
amosli@amosli-pc:~/learn$ echo $no2
4

 

amosli@amosli-pc:~/learn$ result=$[no1+no2]
amosli@amosli-pc:~/learn$ echo $result
8

 

amosli@amosli-pc:~/learn$ result=$((no1+50))
amosli@amosli-pc:~/learn$ echo $result
55

 

amosli@amosli-pc:~/learn$ echo "3*12" | bc
36

 

amosli@amosli-pc:~/learn$ echo "scale=3;10/3" |bc
3.333
amosli@amosli-pc:~/learn$ echo "sqrt(100)" |bc 
10