SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 父级目录(Parent Directory):有时你可能需要获得脚本所在目录的父目录。这可以通过在SCRIPT_DIR上再使用一次dirname命令来实现。 PARENT_DIR=$(dirname "$SCRIPT_DIR") 理解如何确定和使用脚本的位置可以帮助你创建更灵活和可靠的脚本。...
如果需要在Makefile目标中使用更复杂的Bash语法,可以将Bash代码放在一个单独的.sh文件中,并在Makefile目标中调用该脚本。例如: 代码语言:txt 复制 test: @bash my_script.sh 在这个示例中,my_script.sh是一个包含Bash代码的文件,该代码将在test目标被调用时执行。
/bin/bash# Store the script directory in a variablescript_dir=$(dirname$0)echo"The script is located in:$script_dir"# Output:# The script is located in: /path/to/your/script Bash Copy In this example, we’re storing the directory of the script in thescript_dirvariable and then printin...
Make the script executable by running the following command in the terminal: chmod +x test1.sh To execute the script, navigate to the directory where the script is saved and run the following command: ./test1.sh This will list all files in the current directory. Output: abc.sh error.log...
@weekly /path/backup_script.sh 我不会进一步讨论您可以使用 crontab 做什么,因为它不在本文的范围内。 通过本入门指南,您将对什么是 Bash、什么是脚本以及 Bash 中的脚本有什么了解。你可以用 Bash 做很多事情,而且你不需要了解很多关于编程的知识,就可以将不同的 Linux 应用程序和工具拼凑在一起,并制作一些有...
#!/usr/bin/env bash script_dir=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd) echo $script_dir 其中第3行代码,无论从何处调用它,都将为你提供脚本的完整目录路径。测试结果如下: 另外,可以根据第一种方法结合使用 realpath 命令,也可获取脚本所在目录的绝对路径: ...
如果你尝试运行这个脚本,你会得到不可理喻的错误消息 script.sh: line 3: [: too many arguments. 什么? Bash 解释这个 if 语句为 if [ i am awesome == i are awesome],这是6个字符串 (i, am, awesome, i, are, awesome) 无意义的 if 语句。 正确的写法是 ...
declare -rxSCRIPT=${0##*/} # Make sure there is atleast one parameter or accessing $1 # later will be anerror. if [ $# -eq 0 ] ; then printf “%s\n” “Type--help for help.” exit 192 fi # Process the parameters while [ $# -gt 0 ] ; do ...
Now open a terminal and run the script like this: /where/i/saved/it/hello_world.sh Hopefully you should have seen it print Hello, World onto your screen. If so well done! That is your first Bash script. TIP If you type: pwd You will see the directory that you are currently ...
Now make the file hello.sh executable byusing the chmod commandas follows: chmod u+x hello.sh And finally, run your first shell script by preceding the hello.sh with your desired shell “bash”: bash hello.sh You'll seeHello, World!printed on the screen. That was probably the easiest ...