Experimental three shell program design: Shell Foundation
Experimental purposes:
Further consolidate the basic grammar of Shell programming language and deepen the understanding of the learned knowledge.
Experimental requirements
1. Use of four kinds of variables
2. Configure Environment variables
3. Wildcards expressions the expression of the meta character
4. Quotes
1. Local Variables
$ var1= "Hello Linux"//define local variable Var1
$ read VAR2//define local variable VAE2
$ echo $var 1 $var 2//display value of local variable
2. Environment variables
$ env//display system environment variables
$ echo $PATH//Show search Path variable path
$ myname= "Zhangs"//define local variable MyName
$ export myname//export local variable myname to environment variable
The difference between the scope of the environment variable and the local variable is observed below
$ echo $var 1 $MYNAME//display local variables and environment variables
$ bash//create child process Bash
$ echo $var 1 $MYNAME//display local variables and environment variables
$ exit//Quit child Process bash
$ echo $var 1 $MYNAME
Modifying an environment variable PS1 the prompt
$ ps1=$//change prompt to $
3. Position variable
Edit the following shell script using Editor VI
#!/bin/bash
Echo $ $ $
Save the two lines of script as a file locat.sh
$ chmod +x locat.sh//Increase execution permissions
$./locat.sh you and me he
Observe the results and understand the definition of position variables
4. Predefined variables
Edit the following shell script with editor VI
#!/bin/bash
Echo $#
Echo $*
Save the above three lines as files predef.sh
$chmod +x predef.sh//Increase execution permissions
$./predef.sh Linux Unix Windows
Observe the results of the operation and understand the meaning of the predefined variables
5. Configure Environment variables
The system's startup parameters can be changed through a configuration file in the user's home directory, for example, by modifying. Bash_profile to modify the environment variable path, as follows:
$ cd ~/Enter user's home directory
$ vi. bash_profile//Edit configuration file
If the following lines are in the file:
Path= $PATH; $HOME/bin
Adds the current path to the path path and modifies the following:
Path= $PATH: $HOME/bin:.
$ exit//exit
Log on to the system with the user's account
Executing the locat.sh script in the current directory does not require the current path to be established, for example:
$ locat.sh MyName is Zhangs
6. Meta-characters and regular expressions
$ cd ~/Enter user home directory
# Ls-l/>list//To list all file information in the root directory in detail, redirect to list file
$ grep ^d List//query all catalogs
$ ls *.sh//List all the shell script files with the suffix named. sh
6. Inverted quotes, single quotes, and double quotes
$ abc= "pwd"
$ Echo ' $ABC '//display only strings within single quotes
$ echo "$ABC"//Display the value of a variable
$ Echo ' $abc '/' and ' integrated use
$ echo "' pwd '"/"inside" to explain
$ echo *
$ echo "*"//mask specific meaning of meta characters
$ Echo ' * '//observation display result
$ echo \*//observation display results