Using the bash shell on CentOS 6

Bash Command-line Editing
Early shell environments did not provide any form of line editing capabilities. This meant that if you spotted an error at the beginning of a long command line you were typing, you had to delete all the following characters, correct the error and then re-enter the remainder of the command. Fortunately Bash provides a wide range of command line editing options as outlined in the following table:

Key Sequence Action
Ctrl-b or Left Arrow Move cursor back one position
Ctrl-f or Right Arrow Move cursor forward on position
Ctrl-p or Up Arrow Scroll back through previously entered command
Ctrl-n or down Arrow Forth through previously entered command
!1030(command's history number) Execute the history command
Delete Delete character currently beneath the cursor
Backspace Delete character to the left of the cursor
Ctrl-_ Undo previous change (can be repeated to undo all previous changes)
Ctrl-a Move cursor to the start of the line
Ctrl-e Move cursor to the end of the line
Meta-f or Esc then f Move cursor forward one word
Meta-b or Esc then b Move cursor back on word
Ctrl-l Clear the screen of everything except current command
Ctrl-k Delete to end of line from current cursor position
Meta-d or Esc then d Delete to end of current word
Meta-DEL or Esc then DEL Delete beginning to current word
Ctrl-w Delete from current cursor position to previous white space

Filename Shorthand

# To display the content of a text file named list.txt
cat list.txt

# The content of multiple text files could be displayed by specifying all the file names as arguments:
cat list.txt list2.txt list3.txt list4.txt

# Pattern matching can be used to specify all file with names matching certain criteria. For example, the "*" wildcard character to can be used to simplify the above example:

cat *.txt

# This could be further restricted to any file names beginning with list and ending in .txt:
cat list*.txt

# Single character matches may be specified using the '?' character:

cat list?.txt

File Name and Path Completion

In order to use filename completion, simply enter the first few characters of the file or path name and the press the ESC key twice. The Shell will then complete the filename for you with the first file or path name in the directory that matches the characters you entered.

To obtain a list of possible matches, press "ESC + =" after entering the first few characters.

Input and Output Redirection

Many shell commands output information when executed. By default this output goes to a device file called stdout which is essentially the terminal window or console in which the shell is running. Conversely, the shell takes input from a device file named stdin, which by default is the keyboard.

Output from a command can be redirected from stdout to a physical file on the file system using the '>' character. For example, to redirect output from an 'ls' command to a file named files.txt, the following command would be required:

ls *.txt > files.txt

To redirect the contents of a file as input to a command:

wc -l < files.txt

The above command will display the number of lines contained in files.txt file.

It is important to note that > redirection operator creates a new file, or truncates an existing file when used. In order to append to an existing file, use the >> operator:

ls *.dat >> files.txt

Whilst output from a command is directed to stdout, any error messages generated by the command are directed to stderr. This means that if stdout is directed to a file, error messages will still appear in the terminal. This is generally the desired behavior, though stderr may also redirected if desired using the 2> operator:

ls ddfrdfdfxcsfg 2> errormsg

On completion of the command, an error reporting the fact that the file named ddfrdfdfxcsfg could not be found will be contained in the errormsg file.

Both stderr and stdout may be redirected to the same file using the &> operator:

ls /etc ddfrdfdfxcsfg &> alloutput

On completion of execution, the alloutput file will contain both a listing of the contents of the /etc directory, and the error message associated with the attempt to list a non-existment file.

Working with Pipes in the Bash Shell

In addition to I/O redirection, the shell allows output from one command to be piped directly as input to another command. A pipe operation is achieved by placing the '|' character between two or more commands on a command line. Foe example, to count the number of process running on a system the output from the ps command can be piped through to the wc command:

ps -ef | wc -l

There is no limit to the number of pipe operations that can be performed on a command line. For example, to find the number of lines in a file which contain the name Smith:

cat namesfile | grep Smith | wc -l

Configuring Aliases

As you gain proficiency with the shell environment, it is likely that you will find yourself frequently issuing commands with the same arguments. For example, you may often use the ls command with l and t option:

ls -lt

To reduce the amount of typing involved in issuing a command, it is possible to create an alias that maps to the command and the arguments. For example, to create an alias such taht entering the letter l will cause the ls -lt command to be execute, the following statement would be used:

alias l ='ls -lt'

Environment Variables

Shell environment variables provide temporary storage of data and configuration setting. The shell itself sets up a number of environment variables that may be changed by the user to modify the behavior of the shell. A list of currently defined variables may be obtained using the 'env' command:

$ env

Perhaps the most useful environment variable is PATH. This defines the directories in which the shell will search for commands entered at the command prompt, and the order in which it will do so.

Another useful variable is HOME which specifies the home directory of the current user. If, for example, you wanted the shell to also look for commands in the scripts directory located in your home directory, you would modify the PATH variable as follows:

export PATH=$PATH:$HOME/scripts

Display en existing environment variable using the echo command:

echo $PATH

Create your own enviroment variables using the export command. For example:

export DATAPATH=/data/file

A useful trick to assign the output from a command to an enviroment variable involves the use of back quotes(`) around the command. For example, to assign the current date and time to an environment variable called NOW:

export NOW=date
echo $NOW

If there are environment variable or alias setting that you need to be configured each time you enter the shell environment, they may be added to a file in your home directory name .bashrc. For example, the following example .bashrc file is configured to set up the DATAPATH environment variable and an alias:

# .bashrc

# Source global definitions
if [-f /etc/bashrc]; then
. /etc/bashrc
fi

# User specific aliases and functions
export DATAPATH=/data/files
alias l='ls -lt'

Writing Shell Scripts

So far we have focused exclusively on the interactive nature of the Bash shell. By interactive we mean manually entering commands at the prompt one by one and executing them. In fact, this is only a smell part of what the shell is capable of. Arguably one of the most powerful aspects of the shell involves the ability to create shell scripts. Shell scripts are essentially text files containing sequences of statements that can be executed within the shell environment to perform tasks. In addition to the ability to execute commands, the shell provides many of the programming constructs such as for and do loops and if statements that you might reasonable expect to find in scripting language.

Unfortunately a detailed overview of shell scripting is beyond the scope of this chapter. There are, however, many books and web resources dedicated shell scripting that do the subject much more justice than we could ever hope to achieve here. In this section, therefore, we will only be providing a very small taste of shell scripting.

The first step in creating a shell script is to create a file (for the purpose of this example we name it simple.sh) and add the following as the first line:

#!/bin/sh

The #! is called the shebang and is a special sequence of characters indicating that the path to the interpreter needed to execute the script is the next item on the line (in this case the sh executable located in /bin). This could equally be, for example, /bin/csh or /bin/ksh if either were the interpreter you wanted to use.

The next step is to write a simple script:

#!/bin/sh

for i in *
do
echo $i
done

All this script does is iterate through all the files in the current directory and display the name of each file. This may be executed by passing the name of the script through as an argument to sh:

sh simple.sh

In order to make file executable (thereby negating the need to pass through to the sh command) the chomd command can be used:

chmod +x simple.sh

Once the execute bit has been set on the file's permissions, it may be executed directly. For example:

./simple.sh

source from here

邓福如:你好吗 天气好吗

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,406评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,976评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,302评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,366评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,372评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,457评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,872评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,521评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,717评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,523评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,590评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,299评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,859评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,883评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,127评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,760评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,290评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,331评论 0 23
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,800评论 0 0
  • 想了好久,还是决定写点什么。四年前,刚进大学,在逼哥的带领下入了英雄联盟的坑,后来我入坑,他爬出去了。大一时没有带...
    丁大壮阅读 639评论 0 0
  • 感恩父母养育之恩愿母亲身体健康衣食无忧智慧增长! 感恩麦克各西老师传播金刚智慧!感恩金刚学友真诚分享! 感恩国家安...
    T上善若水阅读 175评论 0 0