In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Shell how to get the absolute path of the current execution of the script, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Pwd command
Let's see if we can get the absolute path to the currently executing script using the pwd command. The function of this command is "print name of current/working directory", which really means the current working directory, not the directory where the script is being executed.
Xiaosi@Qunar:~/company/sh$ cat pwd.shecho `pwd`xiaosi @ Qunar:~/company/sh$ sh pwd.sh/home/xiaosi/company/shxiaosi@Qunar:~/company/sh$ cd.. xiaosi@Qunar:~/company$ sh sh/pwd.sh / home/xiaosi/company
There is only one sentence in pwd.sh script: echo `pwd`. By running the script under different paths, sh pwd.sh gets / home/xiaosi/company/sh, while sh sh/pwd.sh gets / home/xiaosi/company, so the pwd command does not get the directory where the script is being executed.
2. $0
$0 is a special variable in Bash environment, and its real meaning is: Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the-c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero .
The value of $0 is related to how it is called:
(1) call bash with a file, and the value of $0 is the name of the file.
Xiaosi@Qunar:~/company/sh$ cat pwd.sh echo $0xiaosifang QunarGunarVuxx companyUnix sh pwd.sh pwd.sh
(2) start bash with the-c option, and the actual command will be read from a string. If there are other parameters after the string, use a special variable reference starting at $0 (regardless of the path).
(3) in addition, $0 will be set to the name of the file that calls bash (not an absolute path)
3. Positive solution of basepath=$ (cd `dirname $0`; pwd)
Dirname $0, which gets the parent directory of the currently executed script file
Cd `dirname $0`, enter this directory (switch the current working directory)
Pwd, showing the current working directory (after cd execution)
Thank you for reading this article carefully. I hope the article "how to get the absolute path to the current script execution by Shell" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
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.