a、长格式列出
b、列出以'.'开头的文档
c、"classify" files by appending each entry
d、隐藏文档名内的控制字符
e、按修改时间排序
结果:
得到一个含有新aliase 用于清屏,和一个以时间排序ls的aliase。
实验2:改变bash 提示符
假设:
您决定定制您的bash 提示符以显示当前目录的完整路径和shell的历史数目,并作点相应的美化。
任务:
1、在一个终端窗口,显示当前提示符字串的值。注意提示符内的静态ASCII 字符和反斜杠(忽略符)。
$ echo $PS1
2、改变您的提示符为只显示一串静态的字串:
$ PS1='Red Hat Linux -> '
3、那不是很有用,所以恢复成原来传统的提示符-美元符,接着主机名:
$ PS1='\h $'
4、在主机名和$之间插入bash 历史提示符\!。确认在每个中间都有一个空格。
5、现在参考bash 的man page,查询代表当前目录的特别字符。(查找PROMPTING,并注意:
您要找的是表示完整路径的特别字符,而非像默认提示符那样的最后一个目录名)将该特别字
符插入您的PS1 提示符字串,并前缀一个冒号。
6、您定制的提示符应该像下面这个例子相同。假如不是,继续做。
station1:/home/student 21 $
7、在您的.bashrc 中编辑您的PS1 定义,然后打开一个新的终端窗口以确认您的新提示符正常工作。
实验3:配置shell 选项
假设:
您将使用set 和shopt 来定制您的bash shell 环境。
任务:
1、在tty1 以student 登录。查看当前配置过的shell 选项:
$ set -o
2、注意当前的ignoreeof 选项为关(off)。按
3、以student 重登录到tty1 并执行下面的语句,然后测试 ignoreeof 选项:
$ set -o ignoreeof
$
$ Use "logout" to leave the shell
$ set o ignoreeof
$
4、现在使用shopt 命令查看其他的shell 选项:
$ shopt
5、配置并测试 cdspell 选项(注意,看清楚以下命令中故意的拼写错误):
$ cd
$ mkdir test_directory
$ cd test_driectoy
bash: cd: test_driectoy: No such file or directory
$ shopt -s cdspell
$ cd test_driectoy
test_directory
$ pwd
/home/student/test_directory
$ cd ..
$ shopt -u cdspell
$ cd test_driectoy
bash: cd: test_driectoy: No such file or directory
6、您现在看到当您试着从一个shell 提示符执行命令,您可能是在运行:一个外部可执行文档、一个内建的shell 命令、一个aliase或一个shell 函数等等。有时候检查您输入的命令
究竟是执行了什么是很重要的。使用内建的type 命令询问shell 究竟是执行了什么类型的命令:
$ type cat
cat is hashed (/bin/cat)
$ type cls
cls is aliased to 'clear'
$ type shopt
shopt is a shell builtin
$ type while
while is a shell keyword
结果:
对一些shell 选项有了进一步的了解。
实验4:命令替换
1、检测当在shell 提示符下键入sawfish 的命令的全路径。使用shell 的快捷方式重新执行那条命令,加上-ui 使得运行sawfish-ui,然后使用另一种快捷方式使运行sawfish-themer。
$ which sawfish
$ which
$ ^ui^themer
2、以sho 为开头,重复最后一个命令。
$
3、当一个命令包括了另一条以backquote(``)包围起来的命令,bash 先执行包围起来的命令,然后在整个命令执行之前将结果替换在``里面。
使用这种技术执行ls -l 列出一个特定目录,该目录是当nautilus 在shell 提示符下输入时程式所在的路径。记住which nautilus 是先被执行的,然后他的结果替换在命令行上,然后
ls -l 在结果上执行。
$ ls -l `which nautilus`
挑战实验:使用shell 函数
任务:
1、在bash shell 提示符下,打入下列行以建立一个简单的shell 函数:
$ lsbytesum()
>{
> TotalByes=0;
> for Bytes in $(ls -l | grep "^-" | cut -c34-42);
> do
> let TotalBytes=$TotalBytes $Bytes;
> done;
> TotalMeg=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc);
> echo -n "$TotalMeg"
>}
2、检验您新建的函数是否有效:
$ set
...output omitted -- your new function near the bottom...
$ lsbytesum()
{
TotalByes=0;
for Bytes in $(ls -l | grep "^-" | cut -c34-42);
do
let TotalBytes=$TotalBytes $Bytes;
done;
TotalMeg=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc);
echo -n "$TotalMeg"
}
... complete prompt shown to demonstrate change to prompt ...
[student@station1 student]$ PS1="[\u@\h:\w ($(lsbytesum) MB)]$ "
[student@station1:~ (11.971 MB)]$ cd /bin
[student@station1:~ (.476 MB)]$ cd /sbin
[student@station1:~ (.587 MB)]$ cd /etc
[student@station1:~ (.124 MB)]$
3、挑战性问题:
1、要将该函数在每次登录时载入,您需要哪些步骤?
2、要将该函数转换为一个简单的shell script,需要哪些步骤?
结果:
得到一个新的显示当前目录中任何文档总数的shell 提示符。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




