# 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 \; 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 \; 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