pkg=’要解压的包’ dirname=$(tar xvf $pkg) cd $dirname
即 cd 后若跟多个目录,只会转向第一个目录中.
有如下几种方法
- root 权限下,可通过 /dev/null 来清空文件
- 普通用户权限下,通过如下方式清空文件
$ : > 文件
若此时文件不存在,上述命令效果同 touch
在 bash 下,通过
$ echo $UID
可获得当前用户的 uid.
除了可以通过 pwd 命令,还可以通过环境变量 $PWD
不管是在双引号还是单引号中,# 都不是注释的开始
在 bash 中,”:” 作为一个占位符,表示 true.如
$ : $ echo $? // 输出为 0
1) $ echo "{these,words,are,quoted}" 打印的是 $ “these” “words” “are” “quoted”
2) $ cat {file1,file2,file3} > combined_file 把 file1,file2,file3 融合成一个文件
3) $ cp file.{txt,backup} 相当于 $ cp file.txt file.backup
4) $ echo {a..z} 打印从 a 到 z 的全部小写字母 $ echo {0..10} 打印从 0 到 10 的全部数字
在 () 中的命令集合是在一个子 shell 中运行该命令集合,主 shell 不能读取子 shell 中的变量。 在 {} 中的命令集合相当于一个匿名函数,外部可以访问 {} 中的变量.
&> 重定向 stdout 和 stderr 2> 重定向 stderr 1>&2 stdout 重定向到 stderr
$ echo $$
它表示从 stdin 读数据,向 stdout 写数据,如
‘~+’ 表示 pwd,该变量存储的是当前工作目录路径,等同于 $PWD
’-' 变量存储的是上次的目录,等同于 $OLDPWD
'=’ 是正则表达式匹配
$ a=2334 $ b=${a/23/flyer} $ echo $b // 打印 flyer34
A script can export variables only to child processes, that is, only to commands or processes which that pariticular script initates. A script invoked from the command-line cannot export variables back to the command-line environment. Child processes cannot export variables back to the parent processes that spawned them.
包括 $0, $1, … ,
如
$ ls [Tt]est.txt
会匹配 Test.txt 和 test.txt 文件.
即对 $、`、\ (转义符号) 进行转义
返回的是 0,即表明在判断中为 true.
返回对命令的描述
若满足以下语义,则返回 true (在 shell 中是 0)
| -e | file exists |
| -f | file is a regular file |
| -s | file is not zero size |
| -d | file is a directory |
| -c | file is a character device |
| -b | file is a block device |
| -p | file is a pipe |
| -h | file is a symbolic link |
| -L | file is a symbolic link |
| -S | file is a socket |
| -t | file is a file descriptor associated with a terminal device |
| -r | file has read permission |
| -w | file has write permission |
| -x | file has execute permission |
| -O | you are owner of the file |
| -G | group-id of the file is same as yours |
| -N | file modified since it was last read |
| f1 -nt f2 | file f1 is newer than f2 |
| f1 -ot f2 | file f1 is older than f2 |
| f1 -ef f2 | file f1 and f2 are hard links to the same file |
有两种方式
| -eq | [ $a -eq $b ] |
| -ne | [ $a -ne $b ] |
| -gt | [ $a -gt $b ] |
| -ge | [ $a -ge $b ] |
| -lt | [ $a -lt $b ] |
| -le | [ $a -le $b ] |
| < | (($a < $b)) |
| <= | (($a <= $b)) |
| > | (($a > $b)) |
| >= | (($a >= $b)) |
‘' 和 '=’ 等效
| != | [ $a != $b ] |
| < | [ $a < $b ] 按 ASCII 序 |
| > | [ $a > $b ] 按 ASCII 序 |
| -z | [ -z $a ] 字符串为 null,长度为 0 |
| -n | 字符串不为 null |
可有如下几种等价形式: if [ $cond1 ] && [ $cond2 ] if [ $cond1 -a $cond2 ] if [[ $cond1 && $cond2 ] ] (注意最后的 ]] 之间没有空格)
’||’ 的情况与上一样.
不加双引号时,二者是一样的,都表示所有的位置参数, “$*” 把全部的位置参数置于一个字符串中, “$@” 同 $@
$BASH : the path to the bash $BASH_VERSION : the version of Bash installed on the system $EUID : effective user id number $UID : user id number $GROUPS : groups current user belongs to $FUNCNAME : name of the current function $HOSTNAME : 主机名 $HOSTTYPE : identify the system hardware $MACHTYPE : identify the system hardware $OSTYPE : 操作系统类型
$IFS : internal field separator (这个非常有用) 默认是 whitespace (space, tab, newline)
通过 declare (或 typeset) builtins 可以设置 shell 变量的类型。
declare/typeset 的参数:
-r readonly e.g. $ declare -r var1 等同于 $ readonly var1 不能修改只读变量的值,否则 bash 会报错 -i integer e.g. $ declare -i num $ num=1 -a array e.g. $ declare -a indices -f functions e.g. $ declare -f func_name 若 -f 后不加变量名,则会打印当前 shell 中定义的函数 -x export e.g. $ declare -x var This declares a variable as available for exporting outside the environment of the script itself -x var=$value e.g. $ declare -x var=373 The declare command permits assigning a value to a variable in the same statement as setting its properties
可用下面的形式定义数组: $ array=(1 2 3 4) 访问该数组中的每个元素用如下的形式: ${array[0]} ${array[1]} … 索引能够是负值.
修改数组的值的方法如下: $ array[0]=10
产生序列数,如
$ seq 10 // 打印从 1 到 10 的数 $ seq 5 10 // 打印从 5 到 10 的数
在 bash 脚本中,有时如下的命令会执行错误,提示没有那个文件:
LOGFILE=”~/log.txt” echo ‘flyer’ >> $LOGFILE
最佳实践是,用 $HOME 环境变量代替 ‘~’
格式为: ${#string} expr length $string expr “$string” : ‘.*’
如
$ name=’flyer’
$ echo ${#name} // 打印为 5 或 $ expr length $name 或 $ expr “$name” : ‘.*’
格式为: expr match “$string” ‘$substring’ expr “$string”:’$substring’
如
$ stringZ=abcABC123ABCabc $ echo $(expr match “stringZ” ‘abc[A-Z]*.2’) // 打印 8 或 $ echo $(expr “$stringZ” : ‘abc[A-Z]*.2’)
格式为: expr index $string $substring
返回 $substring 第一次出现的索引位置,从 1 开始算. 原理是,对 $string 从左到右匹配,遇到第一个与 $substring 中的第一个字符相同时开 始匹配,若紧接着的字符串和 $substring 的相同,则返回第一个匹配的字符的索引位置. 若从第二个后不匹配,则对 $substring 从第二个字符开始按上述的进行匹配.
格式为:
${string:position}
Extracts substring from $string at $position (索引从 0 开始)
If the $string parameter is “*” or “@”, then this extracts the positional
parameters starting at
${string#substring}
Deletes shortest match of $substring from front of
${string/substring/replacement}
Replace first match of $substring with
- ${parameter-default}, ${parameter:-default}
If parameter not set, use default.
若 parmeter 已经声明了但没有赋值,则 ${parameter-default} 打印为 null,而
{parameter:-default} 会打印 default.
e.g:
$ var1=1
$ var2=2
$ echo ${var1-$var2}
$ echo ${var3-$var2}
$ echo
- ${parameter=default}, ${parameter:=default}
If parameter not set, set it to default. 二者的区别同上.
- ${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string. 二者的区别同上.
- ${parameter?err_msg}, ${parameter:?err_msg}
If parameter set, use it, else print err_msg 区别同上