RH033读书笔记(11)-Lab 12 Configuring the bash Shell
Sequence 1: Configuring the bash Shell
Deliverable: A system with new aliases that clear the screen, and produce a useful timesorted
ls listing.
Instructions:
1. You have decided to create an alias so that when you type c, the system will run the clear
command to clear the screen. Begin by logging in as user student on tty1, then input and test
your alias.
[student@stationX ~]$ alias c='clear'
[student@stationX ~]$ alias
[student@stationX ~]$ c
2. This alias will be lost if you log out and log back in. To ensure that the new alias is
available each time user student logs in, perform the following steps:
[student@stationX ~]$ cd
[student@stationX ~]$ vi .bashrc
Locate the line that contains the text:
# User specific aliases and functions
Add your alias to the line immediately below:
alias c='clear'
Save the file and exit.
3. Test your change by logging out, logging back in on tty1, and typing the following:
[student@stationX ~]$ alias
[student@stationX ~]$ c
4. Create an alias called lr that invokes ls with the following features:
• The alias displays a long listing format.
• It lists all files, including those that beginning with a dot.
• It classifies files by appending a file type indicator to filenames of certain types.
• It sorts the listing by modification time.
• It displays files in reverse order.
You probably will need to consult the man page for the ls command to figure out the
appropriate options. After you have created and tested your alias, add the alias to your
.bashrc file.
alias lr='ls -laptr'
OR
alias lr='ls -laFtr'
Sequence 2: Changing your bash prompt
Scenario: You have decided to customize your bash prompt to display the full path of
the current working directory and the shell history number, and to make some
other cosmetic changes.
Instructions:
1. In a terminal window, display the current value of your primary prompt string. Note the
presence of static ASCII characters and backslash-escaped special characters in the prompt.
[student@stationX ~]$ echo $PS1
2. Change your prompt to print only a static string for testing purposes:
[student@stationX ~]$ PS1='Red Hat -> '
Red Hat ->
Be sure to include a space after the -> so that commands have padding, that is, so that
commands do not appear to touch the prompt (this is almost always desirable with prompts).
3. That prompt is not very useful, so restore the traditional bash dollar-sign, along with the
name of the host:
Red Hat -> PS1='\h $ '
stationX $
4. Insert the bash history prompt special character \! between the hostname and dollar-sign.
Make sure you pad it on each side with a space character. This will insert the number of the
current command in bash's history. The actual number you see will probably be different
than the one shown below.
stationX $ PS1='\h \! $ '
stationX 21 $
5. Now refer to the bash man page to find the special character for the current working
directory. (Search the man page for PROMPTING, and beware: you want the special
character for the full pathname, not the basename as in the default prompt.) Insert that
special character into your PS1 prompt string preceded by a colon.
6. Your customized prompt should appear as shown in the following examples. If not,
continue to work on it. Your completed prompt setting should be something like: PS1='\h:\w \! \$ '
station1:~ 21 $ cd /tmp
station1:/tmp 22 $
7. Edit your new definition of PS1 into your .bashrc, then open a new terminal window to
make sure your new prompt is in effect.
Sequence 3: Command line expansion
Scenario: You have decided to make a backup of the /etc/password file and giving the backup a name which will be useful as an indication of when the file was created.
Instructions:
1. First, you want to find out the last time the file was modified. This will give you a rough idea of how often the file may be updated. The information can be found by making a long listing of the file
[student@stationX ~]$ ls -l /etc/passwd
Given the output of this command you can decide on what date information would be most relevant to include as part of the backup's name. i.e., if you suspect the file is updated several times a week you will need to include more than just the month and year that a backup was made. One date format could be the following:
[student@stationX ~]$ date +%Y%m%d
Experiment with the date command to produce an alternative output which would produce
a four figure representation of the year followed by the month number in two digits and day
of the month in two digits. This would be more useful in that an alphanumeric listing of all
backup files be in date order.
2. Now make a backup of the file appending the output of the date command to the file name.
In order to do this we will take advantage of two bash command line expansion tricks. The
first substitutes the parameter given to the last command typed onto the current command
line, while the second is an alternative syntax to the back tick command execution covered
in this unit.
[student@stationx ~]$
cp /etc/passwd ~/passwd-$(date Alt-.)
3. Lastly we demonstrate a bash trick which allows you to execute the last command from
your history matching a given string.
[student@stationx ~]$ !cp
[student@stationx ~]$ !ls
An alternative to the above would be to type Ctrl-r and begin typing the command line you
want to re-execute. This reverse search through your history shows the match as you type. If
there are multiple matches you can delve further back in your history by repeatedly typing
Ctrl-r until you reach the command you were looking for.
Sequence 4: Command substitution
Instructions:
1. Determine the full pathname of the command executed when metacity is entered at the
shell prompt. Use a shell shortcut to re-execute that command, appended with -message to
run the same command on metacity-message, and then a second shell shortcut to run it on
metacity-window-demo. Remember that Alt- in the instructions below means to press the
Alt key.
[student@stationX ~]$ which metacity
[student@stationX ~]$ which Alt-.-message
[student@stationX ~]$ ^message^window-demo
2. Repeat your last command containing ich.
[student@stationX ~]$ Ctrl-richEnter
3. When a command contains another command enclosed in backquotes (``), bash evaluates
the backquoted command first, and the result is substituted before the full command is
executed.
Use this technique to perform an ls -l listing on the full pathname of the command executed
when nautilus is entered at the shell prompt. Remember that which nautilus is evaluated
first, its result is substituted on the command line, then ls -l is executed on the result.
[student@stationX ~]$ ls -l `which nautilus`
A dollar-sign followed by parentheses can be used to do the same thing as backquotes with
the added advantage that parentheses can be nested. The following example uses echo and
date to insert a datestamp before ls -l's output. Note how the output of which nautilus is
nested within the call to ls, which is in turn nested within the call to echo:
[student@stationX ~]$ echo "$(date): $(ls -l $(which nautilus))"
Challenge Sequence 5: Creating a more versatile backup script
Scenario: You have been asked to write a script called backup.sh. This script will be
similar to the backup-sysconfig.sh script created for earlier labs, but
instead of always backing up the same directory, it will take the directory to be
backed up as an argument.
The script will also be housed in /usr/local/bin instead of /root/bin
so that anyone can execute it.
Instructions:
1. Log in as student and use sudo to open a new file in your text editor:
[student@stationX ~]$ sudo vim /usr/local/bin/backup.sh
2. Remember to insert a comment line explaining what the script does and what arguments it
takes. Do not forget the line containing the magic "shbang" sequence: #!
#!/bin/bash
# A script for backing up any directory
# First argument: The directory to be backed up
# Second argument: The location to back-up to.
3. Next, define some variables with a meaningful names, which will take their values from the
script's first two arguments. This is not strictly necessary. We could just use $1 and $2 the
whole time, but it will make the rest of the script more readable.
ORIG=$1
BACK=$2
4. Next, use a conditional to test whether the destination ($BACK) already exists. If it does,
you can give the user a chance to bail out with a clever use of the read command:
if [ -e $BACK ]
then
echo "WARNING: $BACK exists"
read -p "Press Ctrl-c to exit or Enter to proceed:"
fi
If the user presses Ctrl-c, the whole script, not just read, will quit. If not, the script will continue.
5. Finally, add a line to actually perform the backup and another to print the date, as in your
previous scripts:
cp -av $ORIG $BACK
echo "Backup of $ORIG to $BACK completed at: $(date)"
6. Save your script. It should look something like this:
#!/bin/bash
# A script for backing up any directory
# First argument: The directory to be backed-up
# Second argument: The location to back-up to.
ORIG=$1
BACK=$2
if [ -e $BACK ]
then
echo "WARNING: $BACK exists"
read -p "Press Ctrl-c to exit or Enter to proceed:"
fi
cp -av $ORIG $BACK
echo "Backup of $ORIG to $BACK completed at: $(date)"
7. Create a backups directory in student's home:
[student@stationX ~]$ mkdir ~/backups
8. Make the script executable and test that it works the way you expect. Because the script was
created as root and because /etc/sysconfig contains protected files, both of these will
have to be done with sudo.
[student@stationX ~]$ sudo chmod 755 /usr/local/bin/backup.sh
Password:
[student@stationX ~]$ sudo backup.sh /etc/sysconfig
~/backups/sysconfig-$(date +%Y%m%d)
Challenge Sequence 6: Improving Your Backup Script
Scenario: Note that your new backup.sh script has one major disadvantage relative to
backup-sysconfig.sh. Unlike the previous script, you have to manually
specify the destination you wish to back up to. It would be more convenient
if the script could automatically set $BACK to a reasonable location in ~/
backups/.
Instructions:
1. How might you change the BACK=$2 line of your script so that instead of taking a second
argument, backup.sh automatically sets BACK to the original filename plus -yyyymmdd
in ~/backups/?
In other words, if the first argument is /etc/sysconfig, how could you make the script
automatically back up to ~/backups/sysconfig-yyyymmdd?
HINT: Be careful to only use the file or directory name being backed up, not the entire
path. You want your backup to end up in ~/backups/sysconfig-yyyymmdd, not ~/
backups/etc/sysconfig-yyyymmdd! This can be accomplished with the basename
command. Try running basename /etc/sysconfig to see how it works.
Add this line to your script. Look at the answer section for this sequence if you get stuck.
BACK=~/backups/$(basename $ORIG)-$(date '+%Y%m%d')
2. Save your changes and try running your new, simpler, backup.sh:
[student@stationX ~]$ sudo backup.sh /etc/sysconfig
RH033读书笔记(11)-Lab 12 Configuring the bash Shell的更多相关文章
- RH033读书笔记(5)-Lab 6 Exploring the Bash Shell
Lab 6 Exploring the Bash Shell Sequence 1: Directory and file organization 1. Log in as user student ...
- RH033读书笔记(7)-Lab 8 Introduction to String Processing
Lab 8 Introduction to String Processing Sequence 1: Exercises in string processing 1. Other than the ...
- RH133读书 笔记(3) - Lab 3 Configuring the kernel
Lab 3 Configuring the kernel Goal: Develop skills tuning the /proc filesystem. Gain some experience ...
- RH033读书笔记(10)-Lab 11 Process Control
Lab 11 Process Control Sequence 1: Job Control 1. [student@stationX ~]$ su - 2. Begin some jobs in t ...
- RH033读书笔记(8)-Lab 9 Using vim
Lab 9 Using vim Sequence 1: Navigating with vim 1. Log in as user student 2. [student@stationX ~]$ c ...
- RH033读书笔记(16)-Lab 17 Installation and Administration Tools
Lab 17 Installation and Administration Tools Goal: Become familiar with system configuration tools a ...
- RH033读书笔记(15)-Lab 16 The Linux Filesystem
Lab 16 The Linux Filesystem Goal: Develop a better understanding of Linux filesystem essentials incl ...
- RH033读书笔记(13)-Lab 14 Network Clients
Goal: Practice using a variety of tools to transfer files between your system and a remote system. S ...
- RH033读书笔记(3)-Lab 4 Browsing the Filesystem
Lab 4 Browsing the Filesystem Sequence 1: Directory and File Organization 1. Log in as user student ...
随机推荐
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
先来说一下我们学校的网站: http://jwxt.sdu.edu.cn:7777/zhxt_bks/zhxt_bks.html 查询成绩需要登录,然后显示各学科成绩,但是只显示成绩而没有绩点,也就是 ...
- 解决windows下的mysql匿名登陆无法使用mysql数据库的问题
原文:解决windows下的mysql匿名登陆无法使用mysql数据库的问题 我在windows下安装了mysql,但是不用密码就能登进去,而root明明是有密码的,我用select user()命令 ...
- 杭州电ACM1098——Ignatius's puzzle
这个话题.简单的数学. 对于函数,f(x)=5*x^13+13*x^5+k*a*x,输入k,对于休闲x,一个数字的存在a,使f(x)是65可分. 对于休闲x. 因此,当x = 1时间,f(x) = 1 ...
- bash学习之环境变量
1.查看系统存在的环境变量env 和 export env命令:查看环境变量 [CJP@CJP ~]$ env HOSTNAME=CJP SHELL=/bin/bash HISTSIZE=1000 U ...
- libsvm中的dec_values以及分类结果评分问题
最近一个图像识别项目里需要对分类的结果进行打分,因为使用的是libsvm3.12,一开始决定直接将svm_predict_values函数的dec_values作为评分返回,后来研究了之后才觉得里面有 ...
- C++ Primer笔记7_STL之关联容器
关联容器 与顺序容器不同,关联容器的元素是按keyword来訪问和保存的.而顺序容器中的元素是按他们在容器中的位置来顺序保存的. 关联容器最常见的是map.set.multimap.multiset ...
- JavaScript中的对象(一)
Email:longsu2010 at yeah dot net 最近我和朋友谈起JavaScript中对象的问题.朋友以写JavaScript为生,而且生活的很好,然而我发现他并不真正懂这们语言的某 ...
- Hadoop-2.4.0安装和wordcount执行验证
Hadoop-2.4.0安装和wordcount执行验证 下面描写叙述了64位centos6.5机器下,安装32位hadoop-2.4.0,并通过执行 系统自带的WordCount样例来验证服务正确性 ...
- Java http数据MD5、AES、DES加密
一,数据加密 1.提供了,md5,Hex,Sha等不可逆算法加密 2.AES加密,此加密方式瘦平台影响较重,所以只适合同类平台加密解密 3.DES自定义加密,跨平台,兼容性好 1.org.apache ...
- (017)将一棵二叉查找树重构成链表(keep it up)
给定一棵二叉查找树,设计算法,将每一层的全部结点构建为一个 链表(也就是说, 假设树有D层,那么你将构建出D个链表). 这个题实质是个BFS,可是实现起来有点麻烦,又不像常见的BFS, 所以编写代码时 ...