Need help understanding a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help understanding a function
# 1  
Old 01-10-2014
Question Need help understanding a function

Hello,

I recently started going in depth with the shell, so I started learning from Linux Shell Scripting CookBook, 2nd edition. I am at the first chapter atm, and the author tells to define a function in the ~/.bashrc.

The function is below.

Code:
prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }

And another given version is

Code:
prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1 ;}

The function, can be called like:

Code:
#prepend PATH /opt/myapp/bin

I managed to define it in bashrc and it works, however I want to understand step by step what it does(basically what each part of the code does and what it means) and unfortunately the author doesn't explain.

I know that basically when called, the function adds, the given path, into the variable that is mentioned. If you say prepend PATH /home/usr it will add /home/usr, to the existing content in the $PATH at the end of what is already in the variable.

Can anybody help with how it works step by step? Sorry if this is a very n00b question, but I don't know whom to ask.

Kind Regards,
George

Last edited by bartus11; 01-10-2014 at 08:10 PM.. Reason: Please use code tags.
# 2  
Old 01-10-2014
Code:
prepend() {
    [ -d "$2" ] &&
    eval $1=\"$2\$\{$1:+':'\$$1\}\" &&
    export $1
}

well [ is a command for tests. it's about the same as test.
looking at help test we see -d checks to see if $2 exists as a directory before doing anything else.

&& joins the statements. in A && B, B is only performed if A was true. similarly A || B, B is only performed if A was false. (Like short-circuit evaluation in C) Read more in the bash reference manual, section 3.2.3 Lists of Commands

A clearer version would perhaps be
Code:
prepend() {
    if [ -d "$2" ]; then
        eval $1=\"$2\$\{$1:+':'\$$1\}\" &&
        export $1
    fi
}

eval is rather bad practice, unless absolutely necessary. I personally wouldn't use this function.. So much can go wrong executing user-defined strings as code. But moving past that. It does that: executes the line again... So placing our variables into there we'll see
Code:
prepend PATH /opt/myapp/bin

become as
Code:
eval PATH="/opt/myapp/bin:$PATH"

the single quotes around the colon there isn't very helpful... But neither is this particular function.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Understanding an example of perl map() function

Hello, I have many folders under which there is always a file with the same name, which contains the data I need to process later. A perl oneliner was borrowed perl -e 'print "gene_id\t", join("\t", map {/(.*)\//; $1} @ARGV),"\n";' *_test.trim/level.csvto make a header so that each column... (5 Replies)
Discussion started by: yifangt
5 Replies

4. Shell Programming and Scripting

Problem with (Understanding) function

I have this code #!/bin/bash LZ () { RETVAL="\n$(date +%Y-%m-%d_%H-%M-%S) --- " return RETVAL } echo -e $LZ"Test" sleep 3 echo -e $LZ"Test" which I want to use to make logentrys on my NAS. I expect of this code that there would be output like 2017-03-07_11-00-00 --- Test (4 Replies)
Discussion started by: matrois
4 Replies

5. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

6. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Programming

How to step in one function after the function be executed in gdb?

In gdb, I can call one function with command "call", but how can I step in the function? I don't want to restart the program, but the function had been executed, gdb will execute next statement, and I don't know how to recall the function. (4 Replies)
Discussion started by: 915086731
4 Replies

8. Shell Programming and Scripting

Help needed for understanding a function

There is a function called start: start() { echo -n $"Sending Startup Email: " echo "${RESTARTBODY}" | mutt -s "${RESTARTSUBJECT}" ${EMAIL} RETVAL=$? if ; then touch ${LOCKFILE} success else failure fi echo return ${RETVAL} } Can anyone explain what the bold part of the... (3 Replies)
Discussion started by: proactiveaditya
3 Replies

9. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question