When writing a script in linux, use the relative path through the directory where the script is located -- `dirname $0` under linux

====================================================

2011-09-15 17:38:40| Category: Learning Summary|Report|Font Size Subscription

    

  DOWNLOAD LOFTER MY PHOTO BOOK |

 

[`], the scientific name is "backticks", if it is surrounded by "backticks", it means that what needs to be executed is a command.

For example, `dirname $0`, it means you need to execute the command dirname $0

 

[""] , the content enclosed in double quotation marks has $ (dollar sign: means taking variable name) ` (back quote: means executing command) \ (escape sign: means escape), and the rest only means characters string.

[''], the content enclosed in single quotation marks, all of which represent strings, including the three special characters mentioned above.

 

 

 

Simply doing $ cd `dirname $0` from the command line is meaningless. Because it returns the "." of the current path.

This command only works if it is written in a script file. It returns the directory where the script file is placed, and can locate the relative position (except absolute position) of the program to be run according to this directory.

Create a new test.sh under /home/admin/test/ with the following content:

cd `dirname $0`

echo `pwd`

Then go back to /home/admin/ to execute

sh test/test.sh

operation result:

/home/admin/test

In this way, you can know the location of some files deployed with the script. As long as you know the relative location, you can locate it according to this directory, and you don't need to care about the absolute location. In this way, the portability of the script is improved, and it can be executed by throwing it to any server (if it is a deployment script).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327015258&siteId=291194637
Recommended