dirname和shell常用命令解读
来源:互联网 发布:淘宝直播中控台 编辑:程序博客网 时间:2024/06/10 17:33
$ cd `dirname $0` 和PWD%} 显示当前目录名称
${#var} 替换为变量字符个数$ cd `dirname $0` 和PWD%} 显示当前目录名称
${#var} 替换为变量字符个数
特殊变量
$ 当前SHELL的PID
? 前一个命令的退出状态
! 后台执行的上一个工作的PID
读取用户输入
read
read var
read first .. last
read -a array
read -p pmtstring var
read -r line
变量类型转换
如果变量在声明时没确定类型,则变量根据赋值情况可以动态转换类型,但如果声明时变量类型已经确定,若赋值为其他类型,将导致原有数据丢失或出错。除非重新声明类型。
对于整数类型的变量,则在命令行支持算法扩展,如num=3*4,支持"",不支持空格。
进制
var=16#abc
输出
printf "The number is %.2f/n" 100
echo -ne "hello/nworld/n"
位置参数
$0 1-0 ${10}
$#
$*
"$*"
$@
"$@"
引用
() 命令组,创建子SHELL执行
{} 命令组,不创建子SHELL
' ' 保护所有的元字符不被解析,想打印',必须放在双引号内,或者使用/转义
" " 只允许变量和命令替换,保护其余的元字符不被解析
命令替换
`cmd`
$(cmd)
二者结果都是一个字符串,如果加"",则保留换行,否则丢失换行。
数学表达式扩展
$[ exp ]
$(( exp ))
f
数组
declare -a array=(item1 item2 ...)
数组元素的引用 ${array[i]}
引用整个数组 ${array[*]}
unset array
函数定义
function f()
{
cmd;cmd;
}
I/O重定向
find . name /*.c print > foundit 2>&1
命令行参数
set可用来设置位置参数,使用set --将清除所有位置参数
$*与$@的区别只在于" "时,当$*放在""内时,参数表成为单个字符串,而$@放在""内时,每个参数都被引号括住。
表达式
评估表达式
expr $[3+4] $[ 3+4 ]
let算术扩展
let i=i+1 支持任何C类型的运算符,但只支持整形数运算
bash不支持小数运行,因此需要在bc或者awk中进行相应的运行,再把结果回。由于bash没有浮点型,所以小数是以字符串表示。
测试表达式
测试可以使用test或者单纯的[ expr1 cmp expr2],结果保存于?变量中,即test与[]等价
字符串测试
[ str1 = str2 ]
[ str1 != str2 ]
[ str ]
[ -z str ]
[ -n str ]
[ -l str ]
[ str1 -a str2 ]
!
复合条件测试
[[ p1 && p2 ]]
[[ p1 || p2 ]]
[[ !p2 ]]
整数测试
[ num1 -eq num2 ]
[ numb1 -ne num2 ]
gt ge lt le
文件属性测试
[ file1 nt file2 ]
[ file1 ot file2 ]
[ file1 ef file2 ]
[-d file]
-b
-e
-G
-O
-g
-k
-r -w -x
-s
-t
-u
测试表达可以与let的运算扩展及(( ))中的C型运算扩展是等价的,后者也许更容易理解,C-like。
条件控制
if command
then
command
command
fi
if test expression
then
command
fi
if [ string/numeric expression ] then
command
fi
if [[ string expression ]] then
command
fi
if (( numeric expression ))
if command
then
command(s)
else
command(s)
fi
if command
then
command(s)
elif command
then
commands(s)
elif command
then
command(s)
else
command(s)
fi
空命令
分支跳转
case variable in
value1)
command(s)
;;
value2)
command(s)
;;
*)
command(s)
;;
esac
循环
for variable in word_list
do
command(s)
done
while condition
do
command(s)
done
until command
do
done
构建菜单的select
select program in 'ls -F' pwd date
中断循环
break [n]
continue [n]
捕获信号
信号列表
1) SIGHUP 9) SIGKILL 17) SIGCHLD 25) SIGXFSZ
2) SIGINT 10) SIGUSR1 18) SIGCONT 26) SIGVTALRM
3) SIGQUIT 11) SIGSEGV 19) SIGSTOP 27) SIGPROF
4) SIGILL 12) SIGUSR2 20) SIGTSTP 28) SIGWINCH
5) SIGTRAP 13) SIGPIPE 21) SIGTTIN 29) SIGIO
6) SIGABRT 14) SIGALRM 22) SIGTTOU 30) SIGPWR
7) SIGBUS 15) SIGTERM 23) SIGURG
8) SIGFPE 16) SIGSTKFLT 24) SIGXCPU
trap 'command; command' signal-number-list
trap 'command; command' signal-name-list
trap singal
trap
调试脚本
bash -x
bash -v
bash -n
附录:
常用命令
script myfile
fuser -n tcp 22 获得打开tcp 22的进程
lsof
- dirname和shell常用命令解读
- shell中的basename和dirname
- linux shell命令${}和basename和dirname的学习
- [SHELL]:dirname 命令详解
- basename、dirname shell
- shell小记:dirname
- shell学习-16-dirname
- dirname $0 shell变量
- linux shell dirname basename
- shell basename dirname的使用
- shell中的basename dirname read
- shell脚本积累---`dirname $0`
- php的dirname(__FILE__)和dirname(dirname(__FILE__))
- basename 和 dirname
- linux basename和dirname
- __FILE__和dirname(__FILE__)
- dirname和basename用法
- __FILE__,realpath和dirname,
- 黑马程序员-java学习笔记之交通灯管理系统
- Memcache基础教程
- C++编程思想笔记
- NGUI(3.0.6) - sprite
- uva La 4255 Guess (拓扑排列)
- dirname和shell常用命令解读
- 我们能为SDN做什么?
- MFC中两个对话框之间数据传递
- 夺冠概率
- (36) WeakHashMap
- 安装和使用memcached
- ETl
- Mysql提示you need the SUPER privilege for this operation的解决办法
- TCP实现 文本文件上传