Linux & Shell (2) Write basic shell scripts

When creating a shell script file, you must specify the shell you are using in the first line of the file. The format for this is:
#!/bin/bash
In a normal shell script line, the pound sign (#) is used as a comment line. After indicating the shell, commands are entered onto each line of the file, followed by a carriage return.
The first hurdle to jump is getting the bash shell to find your script file. Shell uses an environment variable called PATH to find commands:
The PATH environment variable is set to look for commands only in a handful of directories. To get the shell to find the shell script, we need to do one of two things: 
  • Add the directory where our shell script file is located to the PATH environment variable. 
  • Use an absolute or relative filepath to reference our shell script file in the prompt.
If we want to have a quick look of PATH, just use: 
$ echo $PATH
Normally, we'll use the second method to tell the shell exactly where the script file is located. (i.e. ./test1)
If we offer the shell script with execution permission, i.e. $ chmod u+x test1, then we can execute this shell script.

1. Print the message

If you want print the message on the screen, just use:
$ echo <message>
If you want to echo a text string on the same line as a command output, you can use the -n parameter for the echo statement to do that. Just change the first echo statement line to this: 
$ echo -n "The time and date are: "

2. Use Variables

2.1 Environment Variables

The shell maintains environment variables that track specific system information, such as the name of the system, the name of the user logged in to the system, the user's system ID.
You can display a complete list of active environment variables available by using the set command:
$ set
You can tap into these environment variables from within your scripts by using $ before the environment variable's name . 
This is demonstrated in the following script: 
$ cat test2
#!/bin/bash
# display user information from the system.
echo “User info for userid: $USER”
echo UID: $UID
echo HOME: $HOME
Look at what happens in this example:
$ echo “The cost of the item is $15”
Then it will print "The cost of the item is 5" 
That is obviously not what was intended. Whenever the script sees a $ sign within quotes, it assumes you're referencing a variable. 
In this example the script attempted to display the variable $1 (which was not defined), so we need to revise the script as: 
$ echo “The cost of the item is \$15”
The cost of the item is $15
TIPS:  You may also see variables referenced using the format ${variable}. The extra braces around the variable name are often used to help identify the variable name from the $ sign.

2.2 User Variables

A shell script allows you to set and use your own variables within the script.  User variables can be any text string of up to 20 letters, digits, or an underscore character. Values are assigned to user variables using an equal sign. 一定注意:变量和等号之间不能有任何空格.  e.g. 
var1=10
var2=-57
var3=testing
var4="still more testing"
The shell script automatically determines the data type used for the variable value. Variables defined within the shell script maintain their values throughout the life of the shell script but are deleted when the shell script completes. 
Just like system variables, user variables can be referenced using the dollar sign: 特别注意,如果需要引用一个变量值是需要用$,而对某个变量进行赋值是不需要。
$ cat test3
#!/bin/bash
# testing variables
days=10
guest=“Katie”
echo “$guest checked in $days days ago”
days=5
guest=“Jessica”
echo “guest checked in $days days ago”
$
Running the script produces the following output: 
Katie checked in 10 days ago 
guest checked in 5 days ago

2.3 The Backtick `

The ` 允许将shell命令的输出赋给变量. While this doesn't seem like much, it is a major building block in script programming. e.g. 
$ cat test5
#!/bin/bash
# using the backtick character
testing=‘date’
echo “The date and time are: ” $testing
The variable testing receives the output from the date command, and it is used in the echo statement to display it. Running the shell script produces the following output:
The date and time are: Mon Jan 31 20:23:25 EDT 2011
一个小例子:

最终的log文件中内容是文件列表的输出

3. Redirecting Input and Output

The bash shell uses > or < for output and input:
command > outputfile or
command < inputfile
此外还有一种内联重定向:<< 。这允许你在命令行而不是文件制定输入重定向的数据。我们会单独设立一个专题来讨论这件事。

4. Pipes(管道)

Sometimes, after the output of a command is redirected to another file, we actually need to move the ouput to another command. 这时,我们需要用到管道pipes,用法为
command1 | command2
The Linux system actually runs both commands at the same time, linking them together internally in the system.例如:
$ rpm -qa | sort > rpm.list 或
$ cat rpm.list | more

5. 退出脚本

Every command that runs in the shell uses an exit status to indicate to the shell that it's done processing. It is an integer value between 0 and 255.

Linux provides the $? special variable that holds the exit status value from the last command that executed.
Besides, the exit command 可以为脚本结束制定一个结束码,类似于return。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值