Explore BrainMass
Share

Shell Scripting

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

How would I write a shell script to locate executable files? This script takes a list of file names from the command line and determines which would be executed had these names been given as commands.

The search path should be based only on the user's PATH environment variable. I don't want to use use the Unix which command, the ksh whence (type) command, or the bash type command.
The script should find only the first occurrence of the "file". If the file is not found, the script should print an error message that the file was not found in the user's path. (Both the filename and the users path should be printed.)
If the first parameter is '-a', then the script should print all occurrences of the executable file in the user's path. Again if the file was not on the path, an error message should be displayed.
I also don't want to use temporary files.

The shell variable PATH defines the search path for the directory containing the command. Alternative directory names are separated by a colon (:). The current directory can be specified by two or more adjacent colons, or by a colon at the beginning or end of the path list.
If the command name contains a / then the search path is not used. Otherwise, each directory in the path is searched for an executable file.
usage: mywhich [-a] command ....

For example:

prompt> mywhich ls
/bin/ls

prompt> mywhich -a cc
/bin/cc
/usr/ucb/cc

prompt> mywhich ./mywhich
./mywhich

prompt> mywhich fooblar
fooblar not found

prompt> mywhich ksh sh csh bash
/usr/bin/ksh
/bin/sh
/bin/csh
/usr/local/bin/bash

© BrainMass Inc. brainmass.com October 16, 2018, 8:29 pm ad1c9bdddf
https://brainmass.com/computer-science/files/shell-scripting-151537

Solution Summary

Shell Scripting is examined.

$2.19
Similar Posting

Shell Scripting: Few Example Bourne Shell Scripts Executions

1. Will the following script execute without any error?

#!/bin/sh

if [ "$USER" ]
then
echo I am $USER
endif

echo Script completed.

2. Does the following script parse the password file and print it out by the user's "realname" "home directory" "login name"?

#!/bin/sh

OLDIFS=$IFS
IFS=:
cat /etc/passwd | while read f1 f2 f3 f4 f5 f6 f7
do
if [ "$f5" ]
then
IFS=,
set -- $f5
echo "$f1 $1 $f6"
IFS=:
fi
done
IFS=$OLDIFS.

View Full Posting Details