Shell scriptGetting started with programming learning is what we will introduce in this article. We can use any text editor, such as gedit, kedit, emacs, vi, etc.Shell scriptIt must start with the following line and be placed in the first line of the file ):
#!/bin/sh...
Note: It is best to use "! /Bin/bash "instead of"! /Bin/sh ", if you use tc shell to change to tcsh, others are similar.
Symbol #! Used to tell the system to execute thisScriptsIn this example, use/bin/sh. After editing and saving, if you want to executeShell script, You must first make it executable:
chmod +x filename
Then, in the directory where the shell script is located, enter./filename to execute the shell script.
Some special symbols in Shell
A []
Shell departure Function
If you have written ComplexShell scriptYou may find that the same code may be used in several places. Using the function is much more convenient. The functions are roughly as follows:
functionname(){# inside the body $1 is the first argument given to the function# $2 the second ...body}
You need to declare the function at the beginning of each script.
The following is a shell script named xtitlebar, which can change the terminal window name. Here we use a function named help, which is used twice in the shell script:
#!/bin/sh# vim: set sw=4 ts=4 et:help(){cat << HELPxtitlebar -- change the name of an xterm, gnome-terminal or kde konsoleUSAGE: xtitlebar [-h] "string_for_titelbar"OPTIONS: -h help textEXAMPLE: xtitlebar "cvs"HELPexit 0}# in case of error or if -h is given we call the function help:[ -z "$1" ] && help[ "$1" = "-h" ] && help# send the escape sequence to change the xterm titelbar:echo -e "33]0;$107"#
InShell scriptIt is a good programming habit to help other users and themselves) use and understand scripts.
Command Line Parameters
We have seen Special variables such as $ * and $1, $2... $9. These special variables include the parameters you input from the command line. So far, we have only learned some simple command line syntax, such as some mandatory parameters and the-h option for viewing help ). However, when writing more complex programs, you may find that you need more custom options. The common practice is to add a minus sign before all optional parameters, followed by a parameter value (such as a file name ).
There are many ways to analyze input parameters, but the example using the case expression below is undoubtedly a good method.
#!/bin/sh help(){ cat << HELP This is a generic command line parser demo. USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2HELP exit 0} while [ -n "$1" ]; docase $1 in -h) help;shift 1;; # function help is called -f) opt_f=1;shift 1;; # variable opt_f is set -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2 --) shift;break;; # end of options -*) echo "error: no such option $1. -h for help";exit 1;; *) break;;esacdone echo "opt_f is $opt_f"echo "opt_l is $opt_l"echo "first arg is $1"echo "2nd arg is $2"
You can run the script like this:
cmdparser -l hello -f -- -somefile1 somefile2
The returned results are as follows:
opt_f is 1opt_l is hellofirst arg is -somefile12nd arg is somefile2
How does this shell script work? The script first loops through all input command line parameters and compares the input parameters with the case expression. If the input parameters match, a variable is set and the parameter is removed. According to the Convention of the unix system, the first input should be the parameter containing the minus sign.
Shell script example
General programming steps
Now let's discuss the general steps for writing a script. Any excellent script should have help and input parameters. Write a framework script framework. sh ).Shell scriptIncluding the framework structure required by most scripts is a very good idea. In this way, when writing a new script, you can first execute the following command:
cp framework.sh myscript
Then insert your own function.
Let's take a look at the two examples below.