Linux & Shell (2) Write basic shell scripts
2014-03-02 16:52
357 查看
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:
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:
If we offer the shell script with execution permission, i.e. $ chmod u+x test1, then we can execute this shell script.
You can display a complete list of active environment variables available by using the set command:
This is demonstrated in the following script:
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:
一定注意:变量和等号之间不能有任何空格. e.g.
Just like system variables, user variables can be referenced using the dollar sign: 特别注意,如果需要引用一个变量值是需要用$,而对某个变量进行赋值是不需要。
Katie checked in 10 days ago
guest checked in 5 days ago
The date and time are: Mon Jan 31 20:23:25 EDT 2011
一个小例子:
最终的log文件中内容是文件列表的输出
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。
#!/bin/bashIn 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 $PATHNormally, 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:
$ setYou 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: $HOMELook 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 $15TIPS: 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: ” $testingThe 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 | command2The 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。
相关文章推荐
- linux shell scripts
- 我的linux学习8&nbsp;shell脚本
- linux中Shell标准输出错误 >/dev/null 2>&1 分析
- Linux&&shell 总结 一
- Linux&&shell 总结 三
- Linux shell I/O重定向详解
- Linux&shell之高级Shell脚本编程-创建函数
- linux-shell-||,&&{},(),reg-命令执行顺序
- Linux&Shell命令系列(二)
- Top DBA Shell Scripts for Monito…
- 【Linux&Unix--open/close/write/read系统调用】
- linux&amp;amp;shell学习(积累中。。。)
- Linux&&shell 总结 一
- Linux执行shell脚本方式及区别&命令后台运行
- Linux&&shell 总结 三
- Linux&Shell命令系列(一)
- Linux login & non-login shell 以及su, sudo相关概念
- Root Android and Install Recovery linux shell script & Android root原理
- linux shell 中"2>&1"含义
- 常用 Linux shell & vim 命令