The name of the currently executing script is: ./BashScript.shWe can verify the value at the first index; we got ./BashScript.sh, the basename of the currently executing script. Using $0 VariableTo get the current script’s directory in a Bash, use the $0 array variable. In Bash, the...
#!/bin/bash echo "The script you are running has basename `basename "$0"`, dirname `dirname "$0"`" echo "The present working directory is `pwd`" Run Code Online (Sandbox Code Playgroud) pwd如果您没有从包含它的目录运行脚本,则单独使用将不起作用. [matt@server1 ~]$ pwd /home/matt ...
/bin/bash# Change the current directory to the script directorypushd$(dirname$0)>/dev/null# Store the current directory in a variablescript_dir=$PWDecho"The script is located in:$script_dir"# Return to the previous directorypopd>/dev/null# Output:# The script is located in: /path/to/yo...
We can get the script’s filename by using the basename command, a built-in command on Bash. To do this, you have to follow the below code. $ cat directory/ScriptName.sh #!/bin/bash echo "Your script name =" $(basename "$0") You can notice that we used the $0 with the base...
Bash: get absolute path to current script Bash shell path relative to current script Bash: while loop - break - continue Functions in Linux shell (bash) Create temporary directory on Linux with Bash using mktemp Count number of lines in a file and divide it by number of seconds in...
Create a new file in the current directory: nano array.sh Combine the freshly learnedforloop with a new indexed array: #!/bin/bash # Create an indexed array IndexedArray=(egg burger milk) #Iterate over the array to get all the values ...
cd $TargetDirectoryecho"Deleting stale files in $TargetDirectory."if["$PWD"!="$TargetDirectory"]then# 防止偶然删错目录.echo"Wrong directory!"echo"In $PWD, rather than $TargetDirectory!"echo"Bailing out!"exit $E_WRONG_DIRECTORYfirm-rf *# 删除文件rm.[A-Za-z0-9]*# 删除点文件echo"Done....
$file/etc/etc: directory #使用-i选项,可以MIME类型的格式显示文件类型信息 $file-i /etc/inittab/etc/inittab: text/plain; charset=us-ascii $file-i /etc/init.d/network/etc/init.d/network: text/x-shellscript; charset=us-ascii $file-i /usr/bin/file/usr/bin/file: application/x-executable...
/bin/bash# 定义进程名和文件名process_name="nginx"file_name="error.log"directory_to_search="/var/log"# 检查进程是否在运行pgrep -x$process_name> /dev/nullif[$?-ne0];then# 如果进程没有在运行,启动它echo"Starting$process_name..."sudo service$process_namestartelseecho"$process_nameis ...
While bash scripting you may require the location of the script to automate various tasks. There are two types of paths in Linux: Relatives Path:Relative is the current working directory. Absolute Path:This is the complete path of the directory and files. ...