Shell programming in the 2nd Chapter Introduction to Shell scripts (bottom)

Source: Internet
Author: User
Tags chmod

1, SH and./The Difference
[email protected] ~]# cat test.sh
Echo I am thzzc1994
[Email protected] ~]# sh test.sh
I am thzzc1994
[Email protected] ~]# bash test.sh
I am thzzc1994
[Email protected] ~]#./test.sh
-bash:./test.sh: Insufficient permissions
Want to let./can execute, need to be in user bit plus permission x (executable exec meaning),
Executing chmod u+x test.sh in my environment is equivalent to chmod 744 test.sh:
[email protected] ~]# ll test.sh
-rw-r--r--1 root root 20 April 11:45 test.sh
[Email protected] ~]# chmod u+x test.sh (chmod 744 test.sh)
[email protected] ~]# ll test.sh
-rwxr--r--1 root root 20 April 11:45 test.sh
[Email protected] ~]#./test.sh
I am thzzc1994
Tip: Because the./method requires a given execution permission each time, but is easily forgotten, and a few more steps, adding complexity, so it is generally performed with SH.
2. The difference between SH and source
[Email protected] ~]# echo ' userdir= pwd' >test.sh
[email protected] ~]# cat test.sh
Userdir= pwd
[Email protected] ~]# sh test.sh
[Email protected] ~]# echo $userdir

The current shell looks at the value of the Userdir and finds that the value is empty. Now take the same steps instead of using source, and then look at the value of the Userdir variable:
[Email protected] ~]# source test.sh
[Email protected] ~]# echo $userdir
/root
Conclusion: The executed script is loaded through source or. The script is executed in the current shell, so after the script ends, the variables (including function) values in the script still exist in the current shell, and the SH and bash execution scripts will start the new child shell execution, and then go back to the parent shell, the variables and the letters Values cannot be persisted.
In the case of Shell scripting, it is best to use. or source to load the script or configuration file, and then load the script later, if there is a requirement to reference or perform other script content or configuration files.
Strike: This is an internet company Linux operation and maintenance job pen test. Echo $user's return result is ()
[email protected] ~]# cat test.sh
User=whoami
[Email protected] ~]# sh test.sh
[Email protected] ~]# echo $user
(A) Current user
(B) thzzc1994
(C) Null value
As already mentioned, the variable value of sh, the parent shell is not get. So this question can be regarded as only one sentence, that is
[Email protected] ~]# echo $user
The result is, of course, a null value.
Conclusion: (1) The child shell script inherits the parent shell's variables, functions and so on, as if the son follows the father's surname, the gene inherits the father's, and vice versa.
(2) If you want the parent shell to inherit the child shell, use the source or the. Load child shell script, and the parent shell will be able to use the variable and function values of the child shell.
3, introduce a simple editing script command cat>, can greatly facilitate the development
cat>test.sh, enter the content and press ENTER, then press Ctrl+d to end the edit.
[Email protected] ~]# cat>test.sh
echo I am thzzc1994 [enter][ctrl+d]
[Email protected] ~]# sh test.sh
I am thzzc1994

Shell programming in the 2nd Chapter Introduction to Shell scripts (bottom)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.