Open a text editor, create a new file, the extension sh (sh for Shell), the extension does not affect the execution of the script, see the name is good, if you write a shell script in PHP, the extension is good with PHP.
Enter some code:
#!/bin/bashecho"Hello world! " "
"#!" is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell. The echo command is used to output text to a window.
There are two ways to run a shell script.
As an executable program
Save the above code as test.sh and CD to the appropriate directory:
chmod +x./test. SH #使脚本具有执行权限. /test. SH #执行脚本
Note, be sure to write./test.sh, not test.sh. Run other binary programs also, the direct write Test.sh,linux system will go to the path to find there is not called test.sh, and only/bin,/sbin,/usr/bin,/usr/sbin, etc. in path, your current directory is usually not in path, So write test.sh will not find the command, to use./test.sh tells the system that it is looking in the current directory.
To run the bash script this way, the first line must be written so that the system can find the correct interpreter.
The "system" here is actually the shell application (imagine Windows Explorer), but I deliberately write the system, is easy to understand, since this system refers to the shell, then a script using/bin/sh as an interpreter can save the first line? Yes.
As an interpreter parameter
This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:
/bin/sh test. sh/bin/php test.php
The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write.
Let's look at an example. The following script uses the read command to get input from stdin and assigns a value to the person variable, and finally outputs on stdout:
- #!/bin/bash
- # Author:mozhiyan
- # Copyright (c) http://see.xidian.edu.cn/cpp/linux/
- # Script follows here:
- Echo "What's your name?"
- Read person
- Echo "Hello, $PERSON"
To run the script:
chmod +x./test. SH $. /test. SH What is your name? Mozhiyanhello, mozhiyan$
"Go" shell tutorial--05 First shell script