在学习linux脚本的时候,如果只是先学习linux命令的话,个人觉得有点慢了。就针对脚本(网上找的例子)学习了。以下我使用的系统为ubuntu12.04
下面是所脚本内容
#!/bin/bash
# alias.sh
#这里路径我就以/usr/include/为例
shopt -s expand_aliases #打开别名功能
# 必须设置这个选项, 否则脚本不会打开别名功能.
# 首先, 来点有趣的.
alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'
Jesse_James
echo; echo; echo; #注意这里有三个回车,即三行空格
alias ll="ls -l"
# 可以使用单引号(')或双引号(")来定义一个别名.
echo "Trying aliased \"ll\":"
ll /usr/include/s*.h #* 别名工作了.
echo
directory=/usr/include/
prefix=s*.h # 看一下通配符会不会引起麻烦.
echo "Variables \"directory\" + \"prefix\" = $directory$prefix"
echo
alias lll="ls -l $directory$prefix"
echo "Trying aliased \"lll\":"
lll # 详细列出/usr/X11R6/bin目录下所有以mk开头的文件.
# 别名能处理连接变量 -- 包括通配符 -- o.k.
TRUE=1
echo
if [ TRUE ]
then
alias rr="ls -l"
echo "Trying aliased \"rr\" within if/then statement:"
rr /usr/include/s*.h #* 产生错误信息!
# 别名不能在混合结构中使用.
echo "However, previously expanded alias still recognized:"
ll /usr/include/s*.h #之前的的别名命令还是可以用的
fi
echo
count=0
while [ $count -lt 3 ]
do
alias rrr="ls -l"
echo "Trying aliased \"rrr\" within \"while\" loop:"
rrr /usr/include/s*.h #* 这里, 别名也不会扩展.
# alias.sh: line 57: rrr: command not found
let count+=1
done
echo; echo
alias xyz='echo $0' # 脚本打印自身内容.
# 注意是单引号(强引用).
xyz
exit 0
2脚本输出的log文件内容
"Alias Jesse James" was a 1959 comedy starring Bob Hope.
Trying aliased "ll":
-rw-r--r-- 1 root root 4730 9月 30 2013 /usr/include/sched.h
-rw-r--r-- 1 root root 5318 9月 30 2013 /usr/include/search.h
-rw-r--r-- 1 root root 2482 9月 30 2013 /usr/include/semaphore.h
-rw-r--r-- 1 root root 4129 9月 30 2013 /usr/include/setjmp.h
-rw-r--r-- 1 root root 1431 9月 30 2013 /usr/include/sgtty.h
-rw-r--r-- 1 root root 5302 9月 30 2013 /usr/include/shadow.h
-rw-r--r-- 1 root root 13662 9月 30 2013 /usr/include/signal.h
-rw-r--r-- 1 root root 6767 9月 30 2013 /usr/include/spawn.h
-rw-r--r-- 1 root root 264 9月 30 2013 /usr/include/stab.h
-rw-r--r-- 1 root root 8536 9月 30 2013 /usr/include/stdint.h
-rw-r--r-- 1 root root 2873 9月 30 2013 /usr/include/stdio_ext.h
-rw-r--r-- 1 root root 31645 9月 30 2013 /usr/include/stdio.h
-rw-r--r-- 1 root root 34259 9月 30 2013 /usr/include/stdlib.h
-rw-r--r-- 1 root root 22612 9月 30 2013 /usr/include/string.h
-rw-r--r-- 1 root root 4763 9月 30 2013 /usr/include/strings.h
-rw-r--r-- 1 root root 3111 9月 30 2013 /usr/include/stropts.h
-rw-r--r-- 1 root root 5027 2月 28 2013 /usr/include/sudo_plugin.h
-rw-r--r-- 1 root root 25 9月 30 2013 /usr/include/syscall.h
-rw-r--r-- 1 root root 5232 9月 30 2013 /usr/include/sysexits.h
-rw-r--r-- 1 root root 24 9月 30 2013 /usr/include/syslog.h
Variables "directory" + "prefix" = /usr/include/s*.h
Trying aliased "lll":
-rw-r--r-- 1 root root 4730 9月 30 2013 /usr/include/sched.h
-rw-r--r-- 1 root root 5318 9月 30 2013 /usr/include/search.h
-rw-r--r-- 1 root root 2482 9月 30 2013 /usr/include/semaphore.h
-rw-r--r-- 1 root root 4129 9月 30 2013 /usr/include/setjmp.h
-rw-r--r-- 1 root root 1431 9月 30 2013 /usr/include/sgtty.h
-rw-r--r-- 1 root root 5302 9月 30 2013 /usr/include/shadow.h
-rw-r--r-- 1 root root 13662 9月 30 2013 /usr/include/signal.h
-rw-r--r-- 1 root root 6767 9月 30 2013 /usr/include/spawn.h
-rw-r--r-- 1 root root 264 9月 30 2013 /usr/include/stab.h
-rw-r--r-- 1 root root 8536 9月 30 2013 /usr/include/stdint.h
-rw-r--r-- 1 root root 2873 9月 30 2013 /usr/include/stdio_ext.h
-rw-r--r-- 1 root root 31645 9月 30 2013 /usr/include/stdio.h
-rw-r--r-- 1 root root 34259 9月 30 2013 /usr/include/stdlib.h
-rw-r--r-- 1 root root 22612 9月 30 2013 /usr/include/string.h
-rw-r--r-- 1 root root 4763 9月 30 2013 /usr/include/strings.h
-rw-r--r-- 1 root root 3111 9月 30 2013 /usr/include/stropts.h
-rw-r--r-- 1 root root 5027 2月 28 2013 /usr/include/sudo_plugin.h
-rw-r--r-- 1 root root 25 9月 30 2013 /usr/include/syscall.h
-rw-r--r-- 1 root root 5232 9月 30 2013 /usr/include/sysexits.h
-rw-r--r-- 1 root root 24 9月 30 2013 /usr/include/syslog.h
Trying aliased "rr" within if/then statement:
1-alias.sh: 行 44: rr: 未找到命令
However, previously expanded alias still recognized:
-rw-r--r-- 1 root root 4730 9月 30 2013 /usr/include/sched.h
-rw-r--r-- 1 root root 5318 9月 30 2013 /usr/include/search.h
-rw-r--r-- 1 root root 2482 9月 30 2013 /usr/include/semaphore.h
-rw-r--r-- 1 root root 4129 9月 30 2013 /usr/include/setjmp.h
-rw-r--r-- 1 root root 1431 9月 30 2013 /usr/include/sgtty.h
-rw-r--r-- 1 root root 5302 9月 30 2013 /usr/include/shadow.h
-rw-r--r-- 1 root root 13662 9月 30 2013 /usr/include/signal.h
-rw-r--r-- 1 root root 6767 9月 30 2013 /usr/include/spawn.h
-rw-r--r-- 1 root root 264 9月 30 2013 /usr/include/stab.h
-rw-r--r-- 1 root root 8536 9月 30 2013 /usr/include/stdint.h
-rw-r--r-- 1 root root 2873 9月 30 2013 /usr/include/stdio_ext.h
-rw-r--r-- 1 root root 31645 9月 30 2013 /usr/include/stdio.h
-rw-r--r-- 1 root root 34259 9月 30 2013 /usr/include/stdlib.h
-rw-r--r-- 1 root root 22612 9月 30 2013 /usr/include/string.h
-rw-r--r-- 1 root root 4763 9月 30 2013 /usr/include/strings.h
-rw-r--r-- 1 root root 3111 9月 30 2013 /usr/include/stropts.h
-rw-r--r-- 1 root root 5027 2月 28 2013 /usr/include/sudo_plugin.h
-rw-r--r-- 1 root root 25 9月 30 2013 /usr/include/syscall.h
-rw-r--r-- 1 root root 5232 9月 30 2013 /usr/include/sysexits.h
-rw-r--r-- 1 root root 24 9月 30 2013 /usr/include/syslog.h
Trying aliased "rrr" within "while" loop:
1-alias.sh: 行 57: rrr: 未找到命令
Trying aliased "rrr" within "while" loop:
1-alias.sh: 行 57: rrr: 未找到命令
Trying aliased "rrr" within "while" loop:
1-alias.sh: 行 57: rrr: 未找到命令
1-alias.sh
3、研究的问题
(直接在桌面终端中执行的话,输出的log文件会出现中文乱码。我就将终端切换到tty0中使用root权限执行,然后回到桌面终端,gedit打开log 文件就没有乱码了)
执行上面的命令,chmod +x 1-alias.sh将命令设置为可执行。
然后在终端中输入bash 1-alias.sh >log 2>&1
上面上面命令,将标准错误和标准输出重新定向到log文件中
1)shopt 命令
shopt命令和set命令很相识,
在终端中输入shopt -p 查看该命令可设置的功能
root@cfj:/home/cfj/shell/test# shopt -p
shopt -u autocd
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -u checkjobs
shopt -s checkwinsize
shopt -s cmdhist
shopt -u compat31
shopt -u compat32
shopt -u compat40
shopt -u compat41
shopt -u direxpand
shopt -u dirspell
shopt -u dotglob
shopt -u execfail
shopt -s expand_aliases
shopt -u extdebug
shopt -u extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -u globstar
shopt -u gnu_errfmt
shopt -s histappend
shopt -u histreedit
shopt -u histverify
shopt -s hostcomplete
shopt -u huponexit
shopt -s interactive_comments
shopt -u lastpipe
shopt -u lithist
shopt -u login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nocasematch
shopt -u nullglob
shopt -s progcomp
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u xpg_echo
上面的-u选项表示没有开启这功能,-s表示开启了对应的功能
这里举第一个autocd为例,如果开启这个功能,那么cd输入之后,按tab键,会列出以cd为前缀的命令
如果没有开启这个功能,会列出当前路径下的文件或者目录
至于其它的功能你们可以自己实验一下。
2)alias 别名命令
命令格式
alias 别名=“有效命令”
3)判断语句if语句
if语句的格式为
if expression
then
elif expression
then
command
else
command
fi
这里上面直接将if要判断的条件置true了,这里if后面的expression是一个bool值,也可以是一条命令执行的结果
express常用来判断文件的属性,数值大小等
例如 -d /usr/include/usb.h 判断usb.h头文件是否可以执行,命令的结果就是0或1,是可执行返回0,反之为1
像这样的命令还有
-d 测试是否文件夹
-f 测试是否一般文件
-L 测试是否链接文件
-r 测试文件是否可读
-w 测试文件是否可写
-x 测试文件是否可执行
-s 测试文件是否非空
此外还有数值比较的 参数
-lt :小于
-gt:大于
-eq :是否相等
-z :为0
等等
4)while 语句
格式 while expression
do
comand
done
这里expression只要为真,循环就不会停下,上面的例子中,意思是判断count变量是否小于3,然后在循环中将count+1,这样循环能实现3次
5)位置参数
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数(可大于9个)
$0 表示脚本的名字
$1-9就是 在执行命令时,传递给脚本的位置参数,
$$ 脚本运行的当前进程的ID号
$! 后台运行的最后一个进程的ID号
$@ 与$#相同,但使用时加引号,并在引号中返回每个参数
$- 显示shell使用的当前选项
$? 显示最后命令的退出状态,0表示无错误(这个变量也常常用来打印输出,在脚本调试时标记某个shell命令或某个函数是否正确执行,但是要注意,$?记载的是最近的函数或命令的退出状态,因此打印时应该立即打印以获得正确的信息)