Visit Our UNIX and Linux User Community


Creating a PATH variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a PATH variable
# 8  
Old 12-27-2018
Oh okay I was confused then. So the locations /usr/bin ect are where programs like cp or rm are located. In other words, its similar to importing a library in Python or Java. Once I specify those locations if I call one of the programs it will look there and see if that program exists and if it does it will run the program. I am only wondering because I am attempting to understand what this script is doing. Some parts of it are obvious, other parts are not. However, I find myself wondering why some programs like echo can be called without first specifying a PATH.
# 9  
Old 12-27-2018
Quote:
Originally Posted by Circuits
So the locations /usr/bin ect are where programs like cp or rm are located. In other words, its similar to importing a library in Python or Java. Once I specify those locations if I call one of the programs it will look there and see if that program exists and if it does it will run the program.
Yes, exactly. You could write a script using the mv, cp, rm, etc.programs, which are all located in /usr/bin (or /bin, depending on your system) by every time specifying the complete path to them. (I suggest to have a look - use the ls command, which is also located there and issue the following command, which will give you a similar output to this:

Code:
$ ls -l /usr/bin
total 145352
-rwxr-xr-x 1 root root          96 Nov 12 15:31 2to3-2.7
-rwxr-xr-x 1 root root       10104 Apr 23  2016 411toppm
-rwxr-xr-x 1 root root          39 Feb  5  2018 7z
-rwxr-xr-x 1 root root          40 Feb  5  2018 7za
-rwxr-xr-x 1 root root          40 Feb  5  2018 7zr
[...]

Somewhere in this list you will see all the programs i mentioned. You could always write /usr/bin/mv to call mv, /usr/bin/cp instead of cp and so on. But you can also include /usr/bin in your PATH variable and if you enter mv the shell (this is the program which takes your input and processes it) will, if it can't find mv, have a look in /usr/bin if it is there - and if it is there (actually it is), then it will use it.

Quote:
Originally Posted by Circuits
I am only wondering because I am attempting to understand what this script is doing. Some parts of it are obvious, other parts are not.
Why don't you just post the script (enclosed in CODE-tags, please)? We can go over it together and answer your questions. Actually what we appreciate most are people willing to learn something for themselves instead of relying on us to do their work. I'd like to understand.... is a highly regarded goal here, infintely higher than please write for me a script which does....

Quote:
Originally Posted by Circuits
However, I find myself wondering why some programs like echo can be called without first specifying a PATH.
This is actually an excellent question! The reason is that not everything you encounter in a script is a "command". There are three (four) distinct types of "things" (for lack of a better word) in a script (apart from such things as variable assignments, calculations and similar things):

1) "reserved words". These are basically what makes for the script language: while...do....done, if...then...else....fi and others are such "reserved words".

2) "built-in commands" or "built-ins", for short: over time it showed that some commands were used so often that the effort needed to load it from external so that it can be executed slowed down considerably the execution of shell scripts. This is why most shells (re-)created these programs inside them so that they could be used without having to load them as external programs. These are "built-ins" because this exactly is what they are: commands but built into the shell already. An example would be the echo command. In fact there is a /usr/bin/echo (or /bin/echo) program you can use but there is also a built-in command echo in most shells. Because built-ins take precedence over external commands you can use the external program by specifying its full path (regardless of what your PATH variable says), but if you use echo without a path then the built-in is used.

3) external commands: These are the programs i told you about before. If you want to use them you either have to specify their full path or put the path were they are located in your PATH variable. Notice that built-ins and reserved words do (for obvious reasons) NOT need any path to be found.

4) aliases: you can create an "alias" for oftenly used commands, even for commands with a certain set of options. For instance when i use the ls command most times i use ls -lai (long form, show hidden files, show the inode number). Since i do not always want to type ls -lai (after all this are seven!!! keystrokes - way too much) i defined an alias:

Code:
alias l='/usr/bin/ls -lai'

and now i can use l and ls -lai would be executed. Think of an alias as something like a macro in other programming languages or a preprocessor statement in C.

You can find out which type the word you want to use is using the type command. In fact this is also an alias (for the bult-in command whence -v), which is built into the shell, so that it is set by default - at least in the shell i use (Korn shell). In the bash shell the type command is a built-in itself. For what whence does in the Korn shell the bash shell relies on the external /usr/bin/whereis program. Here is what using the type command will look like:

Code:
# type while
while is a keyword

# type whereis
whereis is a tracked alias for /usr/bin/whereis

# type echo
echo is a shell builtin

# type /bin/echo
/bin/echo is /bin/echo

I suggest you try yourself and play around yourself to get acquainted.

I hope this helps.

bakunin

Last edited by bakunin; 12-28-2018 at 07:00 AM..
These 2 Users Gave Thanks to bakunin For This Post:
# 10  
Old 12-28-2018
Sure yeah you guys have all been a great help so far, thanks. I will go ahead and show the script here. I have commented it to show what I understand and what I do not understand. Note that my comments are marked with // to differenciate them from the preexisting comments:
Code:
#!/bin/sh 
# Sound Manager 
#

### BEGIN INIT INFO
# Provides:          soundmanager
# Required-Start:    $local_fs $remote_fs $syslog $network
# Required-Stop:     $local_fs $remote_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Sound Manager
# Description:       Start the Sound Manager daemon
### END INIT INFO


PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/entity/bin //designate the various path's so that the shell knows where to look for executable's
data_dir=/usr/local/entity/project //designate a location where the shell can store information (does not seem to be used in the script)
SOUNDMANAGER=bmsoundmanager //give the bmsoundmanager a variable or an alias in this case "SOUNDMANAGER" the bmsoundmanager 
                            //is a program within entity/bin which controls various aspects
                            //of the sound system, it is an extension of SoundManagerAlsa

 
start() { //the "start" function, first statement in the case statement 
    echo -n "Starting Sound Manager:             " //print statement, will not output the trailing newline
    # Enable audio amplifier mode input
    echo 107 > /sys/class/gpio/export //not completely sure what this does, it might compare the value 107 to a value found in the export directory
    # Mute the amplifier
    echo 0 > /sys/class/gpio/gpio107/value //again not completely sure what this does
      ( ${SOUNDMANAGER} -q -n& ) && echo '[ OK ]' || echo '[FAIL]' //start the bmsoundmanager and feed it the
                                                                   //conditions -q (quiet = true: debug output is muted) and -n (detach = false: do not run as a daemon) 
                                                                   //&& means 'and' while || means 'or'
                                                                    //I am not sure how the program knows to echo OK versus FAIL
}

 
 
stop() { //the "stop" function, second statement in the case statement
    echo -n "Stopping Sound Manager:             " //print statement, will not output the trailing newline
    killall -HUP ${SOUNDMANAGER} //stops the program from running; however, H U P aren't in the man pages for the killall command so I am not sure what they do
}

//Here we have the case statement or the conditional logic. The first condition being that a user entered the command: 'start' and the second being
//that the user entered 'stop' when running the shell. 
case "$1" in
"start")
        start
        ;;
"stop")
        stop
        echo "OK"
    ;;
reload|restart)
        stop
        sleep 1
        start
    ;;
*)
    echo "$0: unknown argument $1." >&2;
    ;;
esac

#

EDIT: I hope the formatting shows up the same on your computer screens as it does on mine. I had some trouble formatting my comments so that it looks neat but you never know how it will appear to other users.

Last edited by Circuits; 12-28-2018 at 03:06 PM..
# 11  
Old 12-28-2018
Hi Circuits,

Just a quick observation, the start function does not seem to be complete. The closing brace seems to be missing as does the command line entry to actually start the daemon.

However saying that there are entries in the script indicating that originally the start function may have been an entry in /etc/inittab, functionallity which I think has been mostly deprecated now.

Regards

Gull04
# 12  
Old 12-28-2018
Quote:
Originally Posted by gull04
Hi Circuits,

Just a quick observation, the start function does not seem to be complete. The closing brace seems to be missing as does the command line entry to actually start the daemon.

However saying that there are entries in the script indicating that originally the start function may have been an entry in /etc/inittab, functionallity which I think has been mostly deprecated now.

Regards

Gull04



I accidentally deleted the closing brace when putting in my comments, that's my fault. Not sure about the command line entry to start the daemon.
# 13  
Old 12-28-2018
Quote:
Originally Posted by Circuits
I will go ahead and show the script here. I have commented it to show what I understand and what I do not understand.
That was a very commendable idea. Now let us go over the script. First, its "big structure":


Code:
#!/bin/sh 
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/entity/bin //designate the various path's so that the shell knows where to look for executable's
data_dir=/usr/local/entity/project //designate a location where the shell can store information (does not seem to be used in the script)
SOUNDMANAGER=bmsoundmanager //give the bmsoundmanager a variable or an alias in this case "SOUNDMANAGER" the bmsoundmanager 
                            //is a program within entity/bin which controls various aspects
                            //of the sound system, it is an extension of SoundManagerAlsa

 
start() {
[...]
}

 
 
stop() {
[...]
}

case "$1" in
"start")
        start
        ;;
"stop")
        stop
        echo "OK"
    ;;
reload|restart)
        stop
        sleep 1
        start
    ;;
*)
    echo "$0: unknown argument $1." >&2;
    ;;
esac

The first thing you need to know is: Every shell script has a "main" part, like every C program has a "main()" function. Alas, the main part is not denoted as such. It is just, what remains after (or rather "outside" of) all the function definitions. I suppose you know from other languages what a function is. Here we have two functions: start() and stop(). One is supposed to start the program, one to stop the program. Functions are just read in and used only when they are called. So, this is the main part, which is executed first when the program is executed:

Code:
#!/bin/sh 
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/entity/bin //designate the various path's so that the shell knows where to look for executable's
data_dir=/usr/local/entity/project //designate a location where the shell can store information (does not seem to be used in the script)
SOUNDMANAGER=bmsoundmanager //give the bmsoundmanager a variable or an alias in this case "SOUNDMANAGER" the bmsoundmanager 
                            //is a program within entity/bin which controls various aspects
                            //of the sound system, it is an extension of SoundManagerAlsa
case "$1" in
"start")
        start
        ;;
"stop")
        stop
        echo "OK"
    ;;
reload|restart)
        stop
        sleep 1
        start
    ;;
*)
    echo "$0: unknown argument $1." >&2;
    ;;
esac

First thing to notice is line 1:

Code:
#!/bin/sh

This tells the operating system which commando processor to use. A typical UNIX (or Linux) system has several possible commando processors (=shells), one of which is the "default shell", which is usually located in /bin/sh. All shells in UNIX are not only a (text-based) surface to the operating system but also a scripting language. Since the scripting languages of the most widely spread shells (the "Bourne shell" and its successors "Korn shell" and "bash") are very similar but not exactly the same this makes sure that the OS does not have to decide which commando processor to use but is told exactly about the one it should call and then feed it the script.

Next thing: "$0", "$1", ...
Code:
[...]
case "$1" in
[...]
    echo "$0: unknown argument $1." >&2;

There are some variables the shell maintains itself and any script can use them. "$0" is the name of the program itself, as it is called. Here is an exercise for you: create a directory in /tmp named "myprogram". Then put a file named "firstprog.sh" there with this content:

Code:
#! /bin/sh
echo  "The name of this program is $0"
exit 0

Then make it executable, here is the whole procedure, which you can cut & paste on the command prompt. How it works should (for now) not be your concern, it uses some advanced features:

Code:
mkdir -p /tmp/myprogram
cat > /tmp/myprogram/firstprog.sh <<-EOF
#! /bin/sh
echo  "The name of this program is \$0"
exit 0
EOF
chmod 754 /tmp/myprogram/firstprog.sh

After this you have a file /tmp/myprogram/firstprog.sh which you can look at:

Code:
$ ls -l /tmp/myprogram
total 4
-rwxr-xr-- 1 bakunin users 57 Dec 28 20:52 firstprog.sh

and which you can exectue. First, change into the directory and call it this way:

Code:
$ cd /tmp/myprogram
$ ./firstprog.sh 
The name of this program is ./firstprog.sh

Now change to your home directory (the cd shell builtin without an argument will get you there) and try again, using the full path:

Code:
$ cd
$ /tmp/myprogram/firstprog.sh 
The name of this program is /tmp/myprogram/firstprog.sh

You see the difference? This is what $0 is for. The other variables with numbers ($1, $2, ...) are the (first, second, ...) representation of the commandline argument(s) given to the program inside of it. We will change our example script a little and see how that works:

Code:
mkdir -p /tmp/myprogram
cat > /tmp/myprogram/firstprog.sh <<-EOF
#! /bin/sh
echo  "The first argument was: \$1"
echo  "The second argument was: \$2"
echo  "The third argument was: \$3"
exit 0
EOF
chmod 754 /tmp/myprogram/firstprog.sh

Now try the following calls of this program one after the other. Observe the result:

Code:
$ /tmp/myprogram/firstprog.sh one two three
$ /tmp/myprogram/firstprog.sh one "two three"
$ /tmp/myprogram/firstprog.sh one "two three" four
$ /tmp/myprogram/firstprog.sh one two three four five
$ /tmp/myprogram/firstprog.sh

What have we seen? First, the variables $1, $2 and so on are filled on a "first come first served" basis. If there are less command line arguments than there are variables some (or maybe even all if there are no arguments) simply are empty. Second: normally the shell is separating arguments along word boundaries: "two" and "three" were separated because there was a white space in between. With the use of quotation (see the second example) you can switch this behavior off and create arguments which contain whitespace. Notice that both double and single quotes will have this effect although there are subtle differences between them which i will not address yet. Now, in light of this, let us try to examine what the main program does:

Code:
#!/bin/sh 
case "$1" in
"start")
        start
        ;;
"stop")
        stop
        echo "OK"
    ;;
reload|restart)
        stop
        sleep 1
        start
    ;;
*)
    echo "$0: unknown argument $1." >&2;
    ;;
esac

You see: the program can be called with a single commandline argument (it can be called with more too but it will ignore them since only "$1" is used). This argument can be one word out of the following:

1) "start"
2) "stop"
3) "reload"
4) "restart"

"start" will call the start function, "stop" will call the stop function, "reload" and "restart" will do the same, namely, first call the stop function, then wait for 1 second ("sleep 1"), then call the start function. Every other value of the first argument will lead to an error message of "unknown argument" and the name of the program so that the user knows which program this error message comes from.

There is nothing more disappointing than having dozens of scripts executed at system start and you get an error message of "failed" without an indication of what failed and why. When you start to program: don't do that to your users. Every script fails from time to time. That is nothing bad. Bad is to deny the user the means to find out why it failed, exactly where it failed and why. A well-written script can do complex things and its output will read like this:

Code:
did thing 1: success
did thing 2: success
did thing 3: failed
cannot do ....
exit 1

A badly written script will just write "failed" and be done. Now find out which of the possibly 25 things the script could do exactly failed and you start to call the programmer every swear name in your vocabulary.

OK, i stop ranting here. You sure are eager to further analyse your script in light of what i said.

I hope this helps.

bakunin

Last edited by bakunin; 12-28-2018 at 05:28 PM..
This User Gave Thanks to bakunin For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Readin document & creating path

Need a way to read a file in who every line is a path to a directory and make shortcut to that directory on a specific place. Example: line in the document /media/gogo/6651-FEAB/Desktop/ /media/gogo/6651-FEAB/Desktop/alex/ /media/gogo/6651-FEAB/linux/ ... (3 Replies)
Discussion started by: gogok_bg
3 Replies

2. Shell Programming and Scripting

File creating in another path.. application unable to locate

I am submitting a concurrent program (of HOST tyme) from Oracle apps screen, The MAIN shell program submits another program, (child) which is also a Shell program. The child writes data to log file. Now the main program, read the log and do some calculations and sends the data to user through... (1 Reply)
Discussion started by: Pradeep Garine
1 Replies

3. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

4. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

5. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. Shell Programming and Scripting

remove a path from PATH environment variable

Hi I need a script which will remove a path from PATH environment variable. For example $echo PATH /usr/local/bin:/usr/bin:test/rmve:/usr/games $echo rmv test/rmve Here I need a shell script which will remove rmv path (test/rmve) from PATH... (9 Replies)
Discussion started by: madhu84
9 Replies

7. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

8. Shell Programming and Scripting

Getting the path from a variable

Hi, I am having a variable Like line="/dir1/dir2/gr3/file.ksh" I need to get the /dir1/dir2/gr3 alone. the no of directories may differ at each time. Please advice. thanks in advance. (3 Replies)
Discussion started by: vanathi
3 Replies

9. UNIX for Dummies Questions & Answers

creating a variable

I cannot seem to get the following script to work. I cannot seem to set the variable. What am I missing? bin/bash set -x echo "2" > /tmp/number STATUS='grep -c 2 /temp/number' if ; then echo "Number 2 is found once" else echo "Number 2 is found more or less than one time" fi (3 Replies)
Discussion started by: rexmabry
3 Replies

10. UNIX for Dummies Questions & Answers

Creating alias for directory path

I am trying to create an alias for a frequently used directory path by using alias xyz="/proj/dir_name" and then trying to reach a sub-directoy by using cd xyz/abc but I get an error saying " No such file or directory " plz tell me wats wrong with this ... (3 Replies)
Discussion started by: jasjot31
3 Replies

Featured Tech Videos