Shell: A weakly typed programming language
What is a weak type and what is a strong type?
- Strongly typed: Variables must be declared beforehand and even initialized before they are used;
- Weak type: variable time declaration, even the type is not differentiated;
Variable assignment, referencing
Assignment Value: Var_name=value
Reference: ${varname} (curly braces can usually be omitted)
Cases:
[root@iZ28g26851kZ ~]# name=test --赋值[root@iZ28g26851kZ ~]# echo $name --引用test[root@iZ28g26851kZ ~]#
Bash Variable type
Environment variables
Export Varname=value; The scope is the current shell process and its child processes.
scripts that are started on the command line inherit the current shell environment variable;
scripts that are automatically executed by the system (non-command-line start-up) need to self-define the required environment variables;
Cases:
[root@iZ28g26851kZ ~]# JAVA_HOME=/opt/java/jdk7/[root@iZ28g26851kZ ~]# export JAVA_HOME[root@iZ28g26851kZ ~]# echo $JAVA_HOME/opt/java/jdk7/[root@iZ28g26851kZ ~]#
Local variables
Set Varname=value: scoped to the entire bash process; (the Set keyword can be omitted)
Local variables
Local Varname=value: Scope is the current code snippet; (Local is a keyword)
Positional variables
$1,$2, ...
A positional variable is a parameter used to refer to a script.
Special variables
Special variables are used to hold some special data.
$?: The execution status return value of the previous command;
Cases:
[[Email protected] ~]# lslibiconv-1.14mhash-0.9. 4. Tar. GZmysql-5.1. Wuyi. Tar. GZnginx-1.8. 0. Tar. GZlibiconv-1.14. Tar. GZmysql-5.1. Wuyinginx-1.8. 0[[Email protected] ~]# echo $?0[[Email protected] ~]# lsss-bash:lsss:command not Found[[email protected] ~]# echo $?127[[Email protected] ~]#
As you can see, when the execution of a command succeeds and fails, $? The values are different
0: Correct execution
1-255: Error execution; (1,2,127 system reserved)
/dev/null
Software devices, bit buckets, data black holes
What do you mean?
When the results of the command execution we do not need to be displayed, we can redirect to/dev/null, the data will not be saved in/dev/null, so no garbage data will be generated
Cases:
[root@iZ28g26851kZ ~]# ls &> /dev/null[root@iZ28g26851kZ ~]# cat /dev/null --可以发现,数据并不存在[root@iZ28g26851kZ ~]# echo $?0[root@iZ28g26851kZ ~]# lsss &> /dev/null[root@iZ28g26851kZ ~]# echo $?127[root@iZ28g26851kZ ~]#
This allows you to only get the state of the command to perform successfully, without needing to know the detailed results of the command execution
Release (undo) variable
Unset VARNAME
Cases:
[root@iZ28g26851kZ ~]# echo $nametest[root@iZ28g26851kZ ~]# unset name[root@iZ28g26851kZ ~]# echo $name[root@iZ28g26851kZ ~]#
When a variable is manipulated, when the $ symbol is added, when the $ symbol is not added
- The action variable itself does not need to be added to the $ symbol
- The value of the action variable is added to the $ symbol
View variables in the current shell
Set does not have any parameters to list all current variables
View Environment variables in the current shell
Printenv
Env
Export
Append a value to a string variable
For the shell, all the default variables are strings
[root@iZ28g26851kZ ~]# mylist=www[root@iZ28g26851kZ ~]# mylist=${mylist}qqq[root@iZ28g26851kZ ~]# echo $mylistwwwqqq[root@iZ28g26851kZ ~]#
So the question is, if you set the environment variables for Java?
[root@iZ28g26851kZ ~]# export PATH=$PATH:${JAVA_HOME}/bin[root@iZ28g26851kZ ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/opt/java/jdk7/bin[root@iZ28g26851kZ ~]#
Of course, this is only temporary, after exiting the current shell, it will expire.
How to make yourself write a file when the script executes
Add it at the beginning of the file #!/bin/bash .
#! Called Shebang (magic number), used to specify the path of the script interpreter, so write /bin/bash it here.
Cases:
#!/bin/bash#这是注释ls -al~ ~
Save the file as list.sh (of course the filename can be written casually)
The file does not have X permission at this time
-rw-r--r-- 1 root root 33 May 712:00list.sh
Add permissions
[root@iZ28g26851kZ ~]# chmod +x list.sh
Okay, now execute,
[root@iZ28g26851kZ ~]# list.sh-bash: list.sh:not found[root@iZ28g26851kZ ~]#
Can't even do it??? ,
Well, actually, when you execute a command, the system defaults to the path of the environment variable to find the executable program.
You can also directly give a path to the executable program,
[Email protected] ~]#/list.sh total 30364dr-xr-x---. 5 root root 4096 May 7 12:03. Dr-xr-xr-x. Root root 4096 Dec 27 23:24. -rw------- 1 root root 12296 may7 : bash_history -Rw-r--r--. 1 root root . Bash_logout -rw-r--r-- 1 root root 7 : Bash_ profile-Rw-r--r--. 1 root root 176 Sep 2004 . BASHRC -Rw-r--r--. 1 root root Sep 2004 . CSHRC -rw------- 1 root root 3 :36
. Lesshst 、、、、、、、、、、、、、、、、、-Rw-r--r--. 1 root root 129 Dec42004 . TCSHRC -rw------- 1 root root 6937 may7 : . V Iminfo [Email protected] ~]#
Haha, this is done!
Linux-based shell programming (1)