User Tools

Site Tools


Action disabled: source
cs:bash_language:example_5
Return to Home page

Example 5

Concepts:
for and if statements

Text:
Implement a bash script that founds if a specific file is inside the directory listed in the current PATH environment variable. The name of the file is passed to the script as the first and only command line argument. When the file is found in one or more directories listed in the PATH environment variable, the script must print the absolute path where the file is located.

In practice, if the PATH environment variable is equal to /usr/sbin:/usr/bin:/sbin, the script must search a specific file_name in the directories: /usr/sbin, /usr/bin and /sbin.

Example:

$ ./ex5.sh ls
/bin/ls

Solution:

bash_ex5.sh
# Implement a bash script that founds if a specific file is inside the directory listed in the current PATH environment variable.
# The name of the file is passed to the script as the first and only command line argument. When the file is found in 
# one or more directories listed in the PATH environment variable, the script must print the absolute path where the file is located.
#
# In practice, if the PATH environment variable is equal to /usr/sbin:/usr/bin:/sbin, the script must search a specific file_name
# in the directories: /usr/sbin, /usr/bin and /sbin.
#
# Example of execution of the script:
# $ ./ex5.sh ls
# /bin/ls
 
 
 
#!/bin/bash
 
if [ $# -ne 1 ] ; then
    echo Usage: $0 \<file_name\>; exit 1
fi
 
 
# A more complex control on the command line parameters (from a functional point of view it is equivalent to the previous check,
# i.e., it is only an example on how to use regular expressions inside an if)
# Double square braces are used to insert regular expressions in the if statement  [[   ]]
# The regular expression reported in the following recognize all the integer positive numbers with the exception of the number 1
# (the number starting with 0, for instance 0123, are not recognized)
if [[ $# =~ 0|[2-9]|[1-9][0-9]+ ]] ; then
    echo Usage: $0 \<nome_file\>; exit 1
fi
 
 
for i in $(echo $PATH | tr ':' ' ')
do
    if [ -e $i/$1 ] # if the path exists
    then
        if [ -f $i/$1 ] # if it is a file
        then echo $i/$1 
        fi
    fi
done

Comments: The PATH environment variable is used by the bash shell as an indicator of the locations where a command typed in a shell can be found. For instance, when the command ls is typed into the terminal, the bash shell search the command ls in the directories listed in the PATH environment variable, and if it found the command the shell execute it.

To see the content of the PATH environment variable the following command can be executed:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

If the PATH variable contains also a dot ., the typed executable command is searched also in the current working directory.

The shell script proposed here, is equivalent to the shell command which:

$ which ls
/bin/ls

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_language/example_5?do=edit
/web/htdocs/www.skenz.it/home/data/pages/cs/bash_language/example_5.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy