文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Bash 备忘录(转载)

Bash 备忘录(转载)

时间:2007-04-16  来源:songbd

(1) 如何输入字面上的Tab、换行字符

可采用形如 $'string'的形式,其中string和C风格中的转义方式类似。比如 echo $'\n' 打印一个换行,
echo "field1"$'\t'"field2"会在field1和field2之间插入一个Tab字符。

(2) 如何判断程序是否运行正确

可通过 $? 获取程序的返回结果,一般返回0为正确。类似的特殊符号还有:

$$ shell的PID
$- shell选项
$! 最近一个后台进程的PID
$0 脚本文件名字
$1-$9 位置参数
$# 位置参数数量
$* 对所有参数求值
$@ 和$*同,除了在使用引号时有区别

(3) 如何在当前shell进程环境运行脚本

默认情况为派生子进程运行你的脚本程序,有时候为了用shell脚本改变当前的运行环境,需要在当前进程运行脚本。方法如下:

. your_script_file

或者

source your_script_file

(4) 如何改变字段的分隔符号

有些脚本需用分隔符号分隔出单词元素,比如 for 循环的使用:

for filename in `ls`; do
echo "myfile: $filename"
done

ls的结果为以行分开的文件名字,for根据分隔符号的定义从输出结果中分隔出每个文件名。
分隔符号定义在环境变量IFS中,默认IFS=$' \t\n',即空格,Tab或者换行,可改成你需要的字符串。

类似的环境变量有:

HOME 登录目录
LOGNAME 用户登录名称
PS1 主提示符号
PS2 次提示符号
PWD 当前工作目录
EDITOR 系统编辑器的路径
OSTYPE 操作系统类型
PPID 父处理程序的PID
RANDOM 每次参考这个变量时都会产生一个随即数
SECONDS shell启动至今经过的秒数
UID 用户的识别码

(5) Bash中如何操作时间

获取某个日期的UNIX时间戳。

$ sec=`date -d "19771003" "+%s"`

时间计算。

$ sec=`expr $sec + 24 \* 60 \* 60`
$ now=`date "+%s"` && dif=`expr $now - $sec`

注意乘法符号*必须要转义。

格式化时间输出:

$ date -d "-$dif seconds" "+%Y%m%d"

注意,-d 参数还可以指定以 days, hours, minutes为单位,并指定加减号表示之后或者之前

这是计算1977年10月3日下一天是几号的方法。

(6) 适用于awk, sed, grep, vi, ex等工具的正则表达式

* 任何数量的任何字符
. 任何一个字符
^ 行首
$ 行尾
[] 含有括号中的字符
[^] 不含有括号中的字符
\ 转义字符
\> 到一个字的最后面
\< 到一个字的最前面
\(...\) 将匹配的字标记起来,待以后再用
x\{m\} x字符重复出现m次
x\{m,\} x字符至少出现m次
x\{m,n\} x字符出现m到n次

(7) 命令执行方式

$ ls; pwd; date

命令依次执行。

$ (ls; pwd; date) >outputfile

所有一次执行的命令输出都定向到outputfile

$ cc p1.c -o p1 && p1

只有在第一个命令执行成功时才执行第二个命令。

$ cc p1.c >& err || mail bobhuang < err

第一个命令执行失败时执行第二个命令

(8) 变量展开编辑子

${variable:-word}
如果variable被设置且是非NULL,则返回variable的值,否则返回word。

比如:

$ v="hello"
$ echo ${v:-world}
hello
$ echo $v
hello
$ echo ${vv:-world}
world
$ echo $vv

${variable:=word}
如果variable被设置且是非NULL,则返回variable的值。否则设置variable的值为word,并返回word

${variable:+word}
如果variable杯设置且非NULL,返回word。否则不执行任何操作,返回空

${variable:?word}
如果variable杯设置且非NULL,返回variable的值,否则打印错误信息word且退出。word可省略。

${variable:offset}
返回variable的子串,偏移从offset开始。

${variable:offset:length}
这个就不用我说了。

(9) 字符串操作

{variable%pattern} 最小化删除variable尾部与pattern相符的部分

$ pathname="/usr/bin/local/bin"
$ echo ${pathname%/bin*}
/usr/bin/local

{variable%%pattern} 最大化删除variable尾部与pattern相符的部分

$ pathname="/usr/bin/local/bin"
$ echo ${pathname%%/bin*}
/usr

{variable#pattern} 最小化删除variable头部与pattern相符的部分
{variable##pattern} 最大化删除variable头部与pattern相符的部分

${#variable} 返回variable的值的字符数。如果是${#*} 或者 ${#@} 则返回位置参数的个数

(10) 函数

function_name () { command ; command ; }
function function_name { command ; command ; }
function function_name () { command ; command ; }
unset function_name
export -f function_name # 把函数导出到子shell

函数可用 return 返回值

(11) Here输入

$ cat <<END
>Hello
>world
>END

(12) 变量定义和用户输入

$ declare -i my_integer_var
$ declare -a my_array_var
$ read answer
$ read first last
$ read -a array_name
$ read -e # 通过编辑器编辑输入行,结果存入系统变量REPLY
$ read -p prompt # 显示提示符号并等待输入,结果存入系统变量REPLY
$ read -r line # 允许输入的数据包含反斜线(\)。

(13) 条件测试

string1 = string2
string1 == string2
string1 != string2
string # string不为null
-z string # string的长度为零
-l # 测试字符串的长度
-n string # string的长度不是零
!string # string不相符
string1 -a string2
string1 -o string2

[[pattern1 && pattern2]]
[[pattern1 || pattern2]]

int1 -eq int2
int1 -ne int2
int1 -gt int2
int1 -ge int2
int1 -le int2
int1 -lt int2

-b filename # 程序模块特殊文件
-c filename # 字符特殊文件
-d filename # 目录存在
-f filename # 文件存在
-g filename # 设置set-group-id
-k filename # 设置sticky bit
-p filename # 文件是命名管道
-r filename # 文件可读
-s filename # 文件大小不是零
-u filename # 设置set-user-id位
-w filename # 文件可写
-x filename # 文件可执行

file1 -nt file2 # file1比file2新
file1 -ot file2 # file1比file2旧
file1 -ef file2 # file1和file2的设备相同或inode相同为true

(14) if 用法

if command 
then
command
fi

if test expression
then
command
fi

if [ string/numeric expression ]
then
command
fi

if [[ string expression ]]
then
command
fi

if command
then
command(s)
else
command(s)
fi

if command
then
command(s)
elif command
then
command(s)
...
else
command(s)
fi

(15) 空命令(null)

null命令用 : 来表示,代表一个命令,该命令不做任何事情,相当于nop操作。主要用来占位。

比如
if ... ; then
...
elif ... ; then
...
else
:
fi

这里符号 : 主要用来占位,满足语法要求,不做任何操作。

$ : ${DATAFILE:=$HOME/junk}

符号:是一个命令,后面的参数是表达式${DATAFILE:=$HOME/junk},虽然:不做任何操作,可是后面的表达式会求值,变量DATAFILE被指定为$HOME/junk

(16) case 用法

case variable in
value1)
command(s)
;;
value2)
command(s)
;;
...
*)
command(s)
;;
esac

注意value1, value2 ...可以用正则表达式,比如 [Gg]ree*)。

(17) for 用法

for variable in word_list
do
command(s)
done

注意word_list可以是正则表达式,比如 for file in memo[1-5] ...

(18) while 用法

while command
do
command(s)
done

(19) until命令

until command
do
command(s)
done

(20) 相关命令

set arg0 arg1 arg2 ... argn # 设置位置参数

shift [n] # 位置参数移位

break [n] # 停止n层循环

continue [n] # 跳过n层循环

exec命令可打开或者关闭标准输入输出,而不用建立一个子SHELL。比如

exec < tmp
while read line
do
echo $line
echo -n "Is this word correct? [Y/N]"
read answer < /dev/tty # read from the terminal
case "$answer" in
[Yy]*)
continue
;;
*)
echo "New word? "
read word < /dev/tty
sed "s/$line/$word/" tmp > error
mv error tmp
esac
done

(21) trap 用法

trap 'command; command' signal-number
当指定的中断发生时,执行引号内的命令。
比如 trap 'rm tmp*; exit 1' 0 1 2 15

trap signal-number
重设信号处理为默认值。
比如 trap 'trap 2' 2会导致按两次^+C才能中止

trap " " signal-number
忽略信号

(22) 排错

sh -x scriptname 变量替换后命令执行前显示命令行
sh -v scriptname 命令执行前显示命令行
sh -n scriptname parse命令行但不运行
set -x 打开 echo
set +x 关闭 echo

(23) getopts用法

#!/bin/sh# Program opts4
#

# z后面的冒号表示z选项后还有一个参数
#
while getopts xyz: arguments 2>/dev/null
do
case $arguments in
x) echo "option -x is specified" ;;
y) echo "option -y is specified" ;;
z) echo "option -z is specified"
echo "\$OPTARG is $OPTARG." ;;
\?) echo "Usage opts4 [-xy] [-z argument]"
exit 1;;
esac
done

echo "The initial value of \$OPTIND is 1."
echo "The final value of \$OPTIND is $OPTIND."

(24) eval命令用法

eval command_string

相关阅读 更多 +
排行榜 更多 +
步行僵尸2无限金币版

步行僵尸2无限金币版

体育竞技 下载
狐狸一号特殊任务无限金币版

狐狸一号特殊任务无限金币版

体育竞技 下载
忍者之雷复仇无限金币钻石版

忍者之雷复仇无限金币钻石版

体育竞技 下载