目录
Getting started
Create an empty file Using touch
-
Primary function of
touch: To modify a timestamp -
Common utility:
To create a file if it does not already exist$ touch hello_world.sh
Chmod
$ bash hello_world.sh
$ ./hello_world.sh (Error: permission denied)
By default, files are not executable.
$ chmod 755 hello_world.sh
$ ./hello_world.sh (Fine!)
$ hello_world.sh (Error: command not found)
-
Why
./Linux systems look for command on the path, not the current directory.
./tells the system don’t bother with the path, look at the current directory.
The Shebang
Tells which command processor should handle this script Must be the first line, or will be ignored as a comment
- For bash
- #!/usr/bin/env
bash(<- recommended) - #!
/bin/bash
- #!/usr/bin/env
- For python
- #!/usr/bin/env
python
- #!/usr/bin/env
- For java script
- #!/usr/bin/env
node
- #!/usr/bin/env
Variables
#!/usr/bin/env bash
NAME="Bob Roberts"
COLOR=Blue
echo Hi $NAME, your favorite color is $COLOR.
Semantics
- Must begin with a letter or an underscore (not a number)
- Are case sensitive (usually all uppercase)
- No spaces either side of the equal sign (double quote sign needed if spaces are needed in the value)
Parameters
Make it possible to interact with the user
#!/usr/bin/env bash
USER_NAME=$1
echo Hello $USER_NAME
echo $(date) # out date and time
echo $(pwd) # print working directory
exit 0
Check if the execution is successful: print the last (most recent) exit code that the system received
echo $?
- $0: the name of the script, the path is included
- $1: the 1st parameter
- $2: the 2nd parameter
- ${10}: the 10th parameter (use of curly braces required for above 9.)
References
LinkedIn Learning: https://www.linkedin.com/learning/learning-linux-shell-scripting-2018
本文介绍了Linux Shell的基础知识,包括如何使用touch创建或修改文件时间戳,chmod设置文件执行权限,以及变量的定义和参数传递。通过实例演示了shebang的重要性,以及如何执行和理解变量在脚本中的作用。
381

被折叠的 条评论
为什么被折叠?



