$ bash < ex1
Can read in
ex1program in and execute
Its general form is:
$ bash script name [parameter]
For example:
$ bash ex2 /usr/meng /usr/zhang
The execution process is the same as the previous method, but the advantage of this method is that it can have parameters after the script name, thereby passing the parameter values to the commands in the program, so that a Shell script can handle multiple situations, just like a function When calling, the corresponding actual parameters can be passed according to the specific problem.
If you start with the current shell (starting with
·means) to execute a Shell script, you can use the following simple form:
$ · ex3 [parameter]
Set the shell script's permissions to executable, and then execute it directly at the prompt.
Specific methods:
$ chmod a+x ex4$ ./ex4
This requirement specifies the specific Shell that executes the script at the beginning of the Shell script, for example
/bin/bash:
#!/bin/bash
The Shell receives the command (script name) entered by the user and analyzes it. If the file is marked executable but is not a compiled program, the shell considers it to be a shell script. The shell will read the contents, interpret them and execute them. So, from the user's perspective, executing a shell script is similar to executing a regular executable file.
Therefore, user-developed shell scripts can reside under a directory on the command search path (usually
/bin,
/usr/binetc.), use it like a normal command. In this way, you develop your own new commands. This method is more convenient if you plan to use the prepared shell script repeatedly.
You can assign the execution result of a command to a variable. There are two forms of command substitution: one is to use backticks to quote the command, its general form is:
command list.
For example: store the full path name of the current working directory in the variable dir and enter the following command line:
$ dir=`pwd`
Another form is:
$(command list). The above command line can also be rewritten as:
$dir=$(pwd)
BashOnly one-dimensional arrays are provided, and the size of the array is not limited. Similar to C language, the subscripts of array elements are numbered starting from 0. To obtain elements in an array, use subscripts. The subscript can be an integer or an arithmetic expression, and its value should be greater than or equal to 0. Users can use assignment statements to assign values to array variables.
The general form of assigning values to array elements is:
Array name [subscript] = value,For example:
$ city[0]=Beijing$ city[1]=Shanghai$ city[2]=Tianjin
Can also be used
declareThe command explicitly declares an array, the general form is:
$ declare -a array name
The general format for reading array element values is:
${array name[subscript]},For example:
$ echo ${city[0]}Beijing
Each element of an array can be assigned element by element using the above method, or it can be assigned in combination. The general form of defining an array and assigning it an initial value is:
Array name = (value 1 value 2 ... value n)
Each value is separated by spaces. For example:
$ A=(this is an example of shell script)$ echo ${A[0]} ${A[2]} ${A[3]} ${A[6]}this an example script$ echo ${ A[8]}
Since there are 7 initial values in the value table, so
AThe number of elements is also 7.
A[8]Exceeds assigned array
Arange, it is considered to be a new element. Since there is no value assigned in advance, its value is an empty string.
If the subscript of the array element is not given, the array name represents the array element with subscript 0, such as
cityis equivalent to
city[0].
use
*or
@When subscripted, all elements in the array will be replaced.
$ echo ${A[*]}this is an example of shell script
$ echo ${#A[*]}7
If you want to write a Shell to find the sum of two numbers, how can you implement it? In order to introduce the use of parameter passing, write a script like this:
$ cat > addlet sum=$1+$2echo $sum
After saving, execute:
$ chmod a+x ./add$ ./add 5 1015
It can be seen that 5 and 10 are passed to
$1and
$2, this is the shell's own preset parameter order. In fact, you can also define the variables first and then pass them in.
For example, modify the above script to get:
let sum=$X+$Yecho $sum
Execute again:
$ X=5 Y=10 ./add15
It can be found that the correct results can also be obtained.
export an environment variable:
$ export opid=True
That's it. If you want it to take effect after logging in, you can add it directly to
/etc/profileor
~/.bashrcInside.
can pass
readTo read a variable value, for example, to wait for the user to enter a value and display it:
$ read -p Please enter a value: input ; echo You entered a value: $input Please enter a value: 21500 You entered a value: 21500
Some important Shell variables should not be modified after assignment, so you can set them to
readonly:
$ oracle_home=/usr/oracle7/bin$ readonly oracle_home
grammar:
test expressionReturns true if the expression is true, otherwise, returns false.
First, the common comparison symbols used in numerical comparisons are given:
-eg =; -ne !=; -gt >; -ge >=; -lt <; -le <=
$ test var1 -gt var2
Whether the file is readable, writable, and executable depends on whether it is an ordinary file and whether it is a directory:
-r; -w; -x; -f; -d
$ test -r filename
The length of the string is zero:
-z; non-zero:-n,like:
$ test -z s1
if string
s1The length is zero and returns true.
equal
s1=s2; not equals1!=s2
There is another way to compare strings (you can compare them in lexicographic order):
$ if [[ 'abcde' < 'abcdf' ]]; then echo yeah, as expected; fiyeah, as expected, yes
The operations that can be performed with this command are:
Arithmetic operations:
+ - * / %;Logical operation:= ! < <= > >=
like:
$ i=5;expr $i+5
in addition,
bcis a command line calculator that can perform some arithmetic calculations.
ifCommand example: If the first parameter is a common file name, then print the file in pages; otherwise, if it is a directory name, enter the directory and print all the files in the directory. If it is not a directory, then prompt relevant information .
if test -f $1then pr $1>/dev/lp0elif test-d $1then (cd $1;pr *>/dev/lp0)else echo $1 is neither a file nor a directoryfi
caseThe command is a multi-way branch command based on pattern matching. The following will determine which set of commands will be executed next based on the user's keyboard input.
while [ $reply!=y ] && [ $reply!=Y ] #The loop statement we will learn below do echo nAre you want to continue?(Y/N)c read reply #Read the keyboard case $replay in ( y|Y) break;; #Exit the loop (n|N) echo nnTerminatingn exit 0;; *) echo nnPlease answer y or n continue; #Return directly to the inner loop and start to continue esacdone
grammar:
while/until command list 1do command list 2done
The difference is that after the former executes command table 1, if the exit status is zero, then execute
doThe following command list 2 will then return to the starting point, and after the latter executes command list 1, it will only perform similar operations if the exit status is non-zero. Same example as above.
grammar:
for variable name in string table do command table done
Example:
FILE=test1.c myfile1.f pccn.hfor i in $FILEdo cd ./tmp cp $i $i.old echo $idone copied
Now let’s take a look at the usage of functions in Shell. Let’s look at an example first: write a function and then call it to display
Hello, World!
$ cat > show# Function definition function show{ echo $1$2;}H=Hello,W=World!# Call the function and pass in two parameters H and Wshow $H $W
Demo:
$ chmod 770 show$./showHello,World!
Did you see something fishy?
$ show $H $W
We can directly follow the function name with the actual parameters.
The order of actual parameters corresponds to the "virtual parameters"
$1,$2,$3…
Note: If you want to pass in a parameter, what should you do if there are spaces in the middle of the parameter? Give it a try first.
to display
Hello World(There is a space between the two words)
function show{ echo $1}HW=Hello Worldshow $HW
If directly
show $HW, definitely not possible, because
$1Only received
Hello, so the results only show
Hello, the reason is that string variables must be used Contain it.
If you are interested, continue learning!
There are many powerful things waiting for you, such as
cut,
expr,
sed,
awketc.