New to Shell Scripting: Need help interpreting example function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New to Shell Scripting: Need help interpreting example function
# 1  
Old 06-16-2016
New to Shell Scripting: Need help interpreting example function

In this example function below, I cannot figure out what certain parts mean.

Code:
if ! echo $PATH

what is "if !"?

Code:
(^|:)$1($|:)

What is
Code:
^|:

and
Code:
$|:

?

Code:
pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
        fi
}

# Path manipulation
if [ `id -u` = 0 ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
fi

pathmunge /usr/X11R6/bin after

unset pathmunge



Moderator's Comments:
Mod Comment Thanks for trying to apply (I)CODE tags - but: use ICODE for partial lines only; CODE tags for code and data lines/paragraphs.

Last edited by RudiC; 06-16-2016 at 11:13 AM.. Reason: Changed ICODE to CODE tags where appropriate.
# 2  
Old 06-16-2016
As glad as we are to help, please consider reading man pages, e.g. man bash/ksh/your shell, to help yourself; those are immense sources of knowledge...!

1) man bash:
Quote:
test expr
If the first argument is ! , the result is the negation of ...
so: if ! means if NOT

2) man regex:
Quote:
A (modern) RE is one(!) or more nonempty(!) branches, separated by '|'
so (^|:)$1($|:) means $1 enclosed in : or at the begin-of-line ( ^ ) or at the end-of-line ( $ )...
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-16-2016
Quote:
Originally Posted by MemberName
In this example function below, I cannot figure out what certain parts mean.

Code:
if ! echo $PATH

what is "if !"?

Code:
(^|:)$1($|:)

What is
Code:
^|:

and
Code:
$|:

?

Code:
pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
        fi
}

# Path manipulation
if [ `id -u` = 0 ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
fi

pathmunge /usr/X11R6/bin after

unset pathmunge

Actually, think it more like this:

Code:
! (echo $PATH | /bin/egrep -q "(^|:)$1($|:)")

The highlighted parenthesis represents how you should see that line of code. They are not intended to be included.
echo $PATH output becomes the input of egrep, which is asked to find a pattern in that output of echo.
It is looking for the following pattern:
Code:
Start of string or character : + argument string + end of string or character :

By placing a ! in front of the whole expression, it turns it into if not found

Look how pathmunge is used below, as an example:
Code:
pathmunge /usr/X11R6/bin after

In English terms:
If /usr/X11R6/bin does not exist append to the path (at the end). That's the after argument.

Last edited by Aia; 06-16-2016 at 01:29 PM..
This User Gave Thanks to Aia For This Post:
# 4  
Old 06-16-2016
Attention, if ! ( ... ) would run ... in a sub-shell. This is extra fork-overhead, and can have some other impact, like a ((var+=1)) will not copy back to the main shell.
Actually this patchmunge() function is in RH/CentOS 5 /etc/profile. And is risky, at least one must quote
Code:
if ! echo "$PATH" | /bin/egrep -q "(^|:)$1($|:)")

And also needs an external program /bin/egrep.
It is improved in RH/CentOS 6:
Code:
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

The functionality of pathmunge is:
it prepends the 1st argument to the PATH (if not yet exists), unless the 2nd argument is "after" where it appends the 1st argument to the PATH (if not yet exists).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to write a function in shell scripting to execute sql files

Hi, I am new to shell scripting and i need to write a automation script to execute sql files. I need to check the table if it is there in system tables and need to write a function to call the .sql files. For ex. I have a.sql,b.sql,c.sql files, where the sql file contains DELETE and INSERT... (1 Reply)
Discussion started by: Samah
1 Replies

2. Shell Programming and Scripting

Mail function in shell scripting

Hi Guys, i'm new to scripting, please help me to write the script. Purpose: To write a simple addition program and to mail the output result. Script: #!/bin/bash echo "entr numbers"; read n1; read n2; answer=$(($n1+$n2)); echo $answer > mail -s "output" karthic324n@gmail.com; ... (4 Replies)
Discussion started by: Karthick N
4 Replies

3. Shell Programming and Scripting

Shell Scripting Function call return value

Hi I have a function : Make_Report() { trx_report=`sqlplus -s $conn_str << @@ set echo off; set pages 0; set feedback off; set verify off; select srv_trx_s_no,... (1 Reply)
Discussion started by: neeraj617
1 Replies

4. UNIX for Dummies Questions & Answers

Interpreting Shell Script errors when called from CRON

Hi All, I am calling a series of shell scripts via CRON so everything is running as root. However, in my error log file I am seeing the following errors. Please can anyone offer any advise as to the possible causes and solution to prevent the errors from appearing. The Error 1227 seems to... (2 Replies)
Discussion started by: daveu7
2 Replies

5. Shell Programming and Scripting

/usr/bin/time Shell Scripting Function

Hello, I have made a Linux Shell Script that downloads 6 files from the Internet and then deletes them. Now i want to use the function "/usr/bin/time" and "bc" to calculate how long the avergate run time for the shell script is. I therefore need to do it 100 times. My shell script code is below: ... (6 Replies)
Discussion started by: solo2
6 Replies

6. Shell Programming and Scripting

Need help interpreting a function

Hi, i was reading through a sample coding and came across this function, can anyone pls help to interpret the code for me. Thank alot find_lines() { res=-1 if ; then grep -i "$@" $FILENAME res=$? fi return $res } (2 Replies)
Discussion started by: Cheranime
2 Replies

7. Shell Programming and Scripting

Insert a function in a jsp file using Shell scripting

Greetings to all.I am new to the forum as well as to UNIX as well.I have a jsp file which has the following selectedStartMonth = request.getParameter( "startMonth" ); selectedStartDay = request.getParameter( "startDay" ); selectedStartYear = request.getParameter( "startYear" );... (3 Replies)
Discussion started by: 20033716
3 Replies

8. Shell Programming and Scripting

Sed function is shell scripting

Hello everybody, I trying to convert a text inside my file that looks something like this: into hyperlink so that the user can click onto it..... I tried this but doesn't work cat mylist9.html |sed -e '<a href="' >mylist13.html Thanks (13 Replies)
Discussion started by: kev_1234
13 Replies

9. Shell Programming and Scripting

Function loading in a shell scripting like class loading in java

Like class loader in java, can we make a function loader in shell script, for this can someone throw some light on how internally bash runs a shell script , what happenes in runtime ... thanks in advance.. (1 Reply)
Discussion started by: mpsc_sela
1 Replies

10. Shell Programming and Scripting

Can't stop shell interpreting special characters

I am struggling with the following sample code: array1=(a b c d) array2=(* * * *) print ${array1} print ${array2} It returns 'c' and the name of a file in the directory I'm in. I can't for the life of me work out how to prevent the shell interpreting the '*' and just get it to return... (2 Replies)
Discussion started by: Doug97
2 Replies
Login or Register to Ask a Question