LinuxShell脚本攻略阅读笔记第1章小试牛刀


一、简介

1.Bash(Bourne Again Shell),目前大多数GNU/Linux系统默认的shell环境。

命令都是在shell终端中输入并执行。打开终端后,提示符的形式:username@hostname$ 或 root@hostname # ($表示普通用户,#表示管理员用户root)

2.shell脚本是一个以#!(shebang)起始的文本文件,如下: #!/bin/bash

shebang是一个文本行,其中#!位于解释器路径之前。/bin/bash是Bash的解释器命令路径。

3.两种运行脚本的方式。

一种是将脚本作为bash到命令行参数

buxizhizhou@ubuntu:~/lssc/chpt1$ bash test_script.sh 
hello,world
另一种是授予脚本执行权限,将其变为可执行文件。chmod a+x script.sh
buxizhizhou@ubuntu:~/lssc/chpt1$ chmod a+x test_script.sh 
buxizhizhou@ubuntu:~/lssc/chpt1$ ./test_script.sh 
hello,world

内核会读取脚本的首行,注意到shebang为#! /bin/bash。它识别出/bin/bash,并在内部像这样执行该脚本:$ /bin/bash test_script.sh

其中,test_script.sh文件内容如下:
#!/bin/bash
echo hello,world

3.5.当启动shell时,它一开始会执行一组命令来定义诸如提示文本、颜色等设置。这组命令在脚本文件~/.bashrc中,对于登录shell则是~/.bash_profile。Bash还维护了一个历史记录文件~/.bash_history保存用户运行过的命令。

4.~表示主目录,通常是/home/user,其中user是用户名;若是root用户,则为/root。

5.登录shell是登录主机后获得的那个shell。如果是登录图形界面环境如GNOME、KDE后打开了一个shell,就不是登录shell。

6.Bash中,每个命令或命令序列是以分号或换行符来分隔。#指明注释的开始,延续到行尾。如 $ cmd1 ; cmd2 等同于 $ cmd1 $ cmd2

二、终端打印

终端是交互式工具,可以通过它与shell环境进行交互。
1.echo.默认在每次调用后添加一个换行符。-n 忽略结尾换行符。

2.echo的三种形式打印:直接打印、单引号、双引号

 

buxizhizhou@ubuntu:~/lssc/chpt1$ echo "Welcome to Bash"
Welcome to Bash
buxizhizhou@ubuntu:~/lssc/chpt1$ echo Welcome to Bash
Welcome to Bash
buxizhizhou@ubuntu:~/lssc/chpt1$ echo 'Welcome to Bash'
Welcome to Bash
三种方法的副作用:

 

直接打印:不能显示文本中的分号——分号在Bash中是命令定界符。echo hello;hello被认为是两个命令echo hello和hello。

单引号:变量替换在单引号中无效。单引号中不进行变量替换。

buxizhizhou@ubuntu:~/lssc/chpt1$ echo $var and '$var' and "$var"
value and $var and value

 

双引号:不能打印叹号!。或将其转义,\!。确切地说,叹号在末尾、或者叹号后没有空格,则不能正常打印。如下面测试,可以看到,叹号后面有空格时还是可以打印的。

buxizhizhou@ubuntu:~/lssc/chpt1$ echo "Hello world !"
bash: !": event not found
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "cannot include exclamation - ! within"
cannot include exclamation - ! within
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "c!c"
bash: !c": event not found
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "c ! c"
c ! c
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "c! c"
c! c
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "c !c"
bash: !c": event not found

 

3.printf.无自动换行符。参数类似于C语言,参数以空格分隔。如下:

 

#!/bin/bash
#文件名:printf.sh

printf "%-5s %-10s %-4s\n" No Name   Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
printf "%-5s %-10s %-4.2f\n" 3 Jeff 77.564
运行后显示:

 

buxizhizhou@ubuntu:~/lssc/chpt1$ bash printf.sh 
No    Name       Mark
1     Sarath     80.35
2     James      91.00
3     Jeff       77.56

4.使用echo和printf,选项要在字符串之前。否则,选项被Bash视为另一个字符串。

echo -e "包含转义序列到字符串"

 

<pre name="code" class="plain">buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "1\t2\t3"
1	2	3
buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "!"
bash: !: event not found
buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "\!"
\!
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "1\t2\t2"
1\t2\t2
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "\!"
\!

彩色输出

buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "\e[1;31m This is red text \e[0m"
 <span style="color:#ff0000;">This is red text</span> 
\e[1;31 将颜色设为红色,\e[0m 将颜色重新置回。替换31就可以设置其他颜色,黑色=40,红色=41,绿色=42,黄色=43,蓝色=44,洋红=45,青色=46,白色=47。

三、变量和环境变量

脚本语言通常不需要在使用变量前声明其类型,直接赋值即可。Bash中,每一个变量的值都是字符串。一些特殊的变量会被shell环境和操作系统用来存储一些特别的值,成为环境变量。

1.使用env命令查看与终端相关到环境变量。

上述命令返回的环境变量以name=value的形式给出,彼此间由null字符(\0)分隔。
$pgrep 进程名 获得该程序的进程ID。
替换命令 tr '\0' '\n' ,将空字符替换为换行符。

izhou@ubuntu:~/lssc/chpt1$ pgrep openvpn
1098
buxizhizhou@ubuntu:~/lssc/chpt1$ sudo cat /proc/1098/environ
UPSTART_INSTANCE=runlevel=2UPSTART_JOB=rcTERM=linuxPATH=/sbin:/usr/sbin:/bin:/usr/binRUNLEVEL=2PREVLEVEL=NUPSTART_EVENTS=runlevelPWD=/previous=NVERBOSE=nobuxizhizhou@ubuntu:~/lssc/chpt1$ sudo cat /proc/1098/environ | tr '\0' '\n'
UPSTART_INSTANCE=
runlevel=2
UPSTART_JOB=rc
TERM=linux
PATH=/sbin:/usr/sbin:/bin:/usr/bin
RUNLEVEL=2
PREVLEVEL=N
UPSTART_EVENTS=runlevel
PWD=/
previous=N
VERBOSE=no
2.变量赋值var=value

var是变量名,value是要赋的值。若value不包含任何空白字符,则不需使用引号;否则必须用单或双引号。
var = value 是相等操作。
使用$变量名或${变量名},获得变量的值。

buxizhizhou@ubuntu:~/lssc/chpt1$ var=value
buxizhizhou@ubuntu:~/lssc/chpt1$ echo $var
value
buxizhizhou@ubuntu:~/lssc/chpt1$ echo ${var}
value
buxizhizhou@ubuntu:~/lssc/chpt1$ var1= value
No command 'value' found, did you mean:
 Command 'pvalue' from package 'radiance' (universe)
value: command not found
buxizhizhou@ubuntu:~/lssc/chpt1$ var1 =value
var1: command not found
buxizhizhou@ubuntu:~/lssc/chpt1$ var1 = value
var1: command not found
3.export设置环境变量。此后从当前shell脚本执行的任何应用程序都继承该变量。

标准环境变量,如PATH。在给出所要执行的命令后,shell会自动在PATH环境变量所包含的目录中查找对应的可执行文件。(目录路径以冒号分隔)PATH通常定义在/etc/environment或/etc/profile或~/.bashrc。若需要在PATH中添加一条新路径,可使用
export PATH="$PATH:/home/user/bin" 或 $ PATH="$PATH:/home/user/bin",$ export PATH。
其他常见环境变量:HOME、PWD、USER、UID、SHELL等

4.获取变量值的长度${#var}

buxizhizhou@ubuntu:~/lssc/chpt1$ var=1234567890
buxizhizhou@ubuntu:~/lssc/chpt1$ echo ${#var}
10
利用PS1环境变量定制提示文本。默认的shell提示文本是在文件~/.bashrc中的某行设置的。
buxizhizhou@ubuntu:~/lssc/chpt1$ cat ~/.bashrc | grep PS1
[ -z "$PS1" ] && return
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
buxizhizhou@ubuntu:~/lssc/chpt1$ PS1="PROMPT>"
PROMPT>
PROMPT>
PROMPT>PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
buxizhizhou@ubuntu:~/lssc/chpt1$ 

一些特殊的字符可扩展成系统参数。如:\u扩展为用户名,\h扩展为主机名,\w扩展为当前工作目录。

四、使用函数添加环境变量



 

相关内容