In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
Different ways of script execution will result in different results, especially on the environment of bash. Scripts can be executed in the following ways.
First write a simple test.sh file:
#! / bin/bashread-p "Please input your firstname:" firstnameread-p "Please input your lastname:" lastnameecho-e "\ nYour full name is: $firstname $lastname"
Use sh test.sh to execute
Use sh test.sh to execute the script file, this method indicates that the shell of sh is used to execute the test.sh file, sh is already a shell replaced by bash, although we declare in test.sh that we use #! / bin/bash to execute our file, but using sh instead of bash at this time, #! / bin/bash no longer works.
Use bash test.sh to execute
This method is actually the same as that of sh test.sh, except that / bin/bash is used to execute our script file.
So, in fact, it is possible to use dash test.sh', depending on what kind of shell you want to use to execute the script, but there are some differences among sh, bash and dash. Some keywords such as let,bash are supported, but sh and dash are not supported, and for some keywords, you choose to use bash.
Use the point. Execution
Before using this method, you must add execution permissions to the file:
$chmod + x test.sh
After adding execute permissions, we can use. / test.sh to execute the script in the same way as bash test.sh, using bin/bash by default to execute our script.
Only this mode of execution requires the addition of execution permissions to the file, but not in other ways.
Use source to execute
Using source, we can also execute our script directly:
Source test.sh
Difference
When we use sh test.sh, bash test.sh,. / test.sh to execute the script, the test.sh running script will use a new shell environment to execute the commands in the script, that is to say, when using these three methods, script is actually executed in the shell of the child process. When the child process is completed, the variables and operations in the child process will end and will not be returned to the parent process.
Can't understand? Look at the following example:
[root@ubuntu] # bash test.shPlease input your first name: yao
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.