Linux in bash_source[0] (go)

Source: Internet
Author: User
Tags parent directory

Transferred from: http://www.cnblogs.com/sunfie/p/5943979.html

The __function__ constant records the name of the current function in C + +. Sometimes it is useful to include this information in the log output. In bash, too, there is a constant funcname, but the difference is that it is an array, not a string, where the first element of the array is the name of the current function. Maybe it's a little hard to understand why funcname an array? Take a look at the example below and you'll see.

1 #!/bin/bash 2  3 function Test_func () 4 {5     echo "Current $FUNCNAME, \ $FUNCNAME = (${funcname[@]})" 6     ano Ther_func 7     echo "Current $FUNCNAME, \ $FUNCNAME = (${funcname[@]})" 8} 9 function Another_func () one {     Ech O "Current $FUNCNAME, \ $FUNCNAME = (${funcname[@]})"}14 echo "out of function, \ $FUNCNAME = (${funcname[@]}) "Test_func17 echo" out of function, \ $FUNCNAME = (${funcname[@]}) "

The result after execution is:

1 out of function, $FUNCNAME = () 2 current test_func, $FUNCNAME = = (test_func main) 3 current Another_func, $FUNCNAM E = (another_func test_func main) 4 current test_func, $FUNCNAME = (test_func main) 5 out of function, $FUNCNAME =&G T ()

So, to be more precise, funcname is an array, but it is maintained in bash as a stack-like form. Another useful constant similar to funcname is Bash_source, which is also an array, but its first element is the name of the current script. This is useful when source, because in the script that is source, $ A is the name of the parent script, not the script name of the source. And Bash_source can be handy.

1 # If the script is sourced by another script2 If [-N ' $BASH _source "-a" $BASH _source "! =" $ "]3 then4     do_something 5 Else # Otherwise, run directly in the shell6     do_other7 fi

Bash_source[0] Bash_source[0] is equivalent to Bash_source, obtaining the path and file name of the currently executing shell file.

Create a new script test.sh under the directory/home/sam/linux programming exercise/shell:

1 #!/bin/bash2 dir= "$ (CD" $ (dirname "${bash_source[0]}")/... /"&& pwd" 3 # dir=$ (CD "$ (dirname ${bash_source[0]})/... /"&& pwd) 4 # dir= ' CD $ (dirname ${bash_source[0]})/... /&& pwd ' 5 echo $DIR 6 echo "OK"

Execution requires a EXECUTE permission: chmod +x test.sh then execute ./test.sh instead ofsh ./test.sh

Tested above write correct: output

1/home/sam/linux Programming Exercise 2 OK

is actually a: 1. Get the full path of the currently executing shell file 2. Executes the dirname and enters the parent directory 3. Process for printing the current working directory

1 # echo "${bash_source[0"} "2 # echo" ${bash_source} "3 # echo" $ (dirname "${bash_source[0]}") "4 # dir=" $ (CD "$ (dirname ${BASH_SOURCE[0]}) "&& pwd" 5 # echo $DIR

Linux in bash_source[0] (go)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.