Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am making simple Shell script form some boring-ever-repeating job. Script changes directory correctly, and now I want to run other scripts based on argument I typed when running script. Other scripts are scattered in subdirectories and each one is unique. So I want to insert just file name and let script find out in which subdirectory is that file.

I tried something like this:

filename="default.ini"
while [ $# -gt 0 ] 
do
    case "$1" in
        -f) filename="$(find ./ -name $2)"; shift;;
        -*) echo >&2 \
            "usage: $0 [-v] [-f file] [file ...]"
            exit 1;; 
        *)  break;;     # terminate while loop
    esac
    shift
done

echo $filename

But, find doesn’t return any result, so it stays default.ini. Is there other way to solve this?

share|improve this question
    
is find's ./ actually what you think is should be? Is find looking in the right directory? –  glenn jackman May 14 at 16:00

1 Answer 1

up vote 1 down vote accepted

I would suggest that this is a more idiomatic way of parsing options:

filename="default.ini"
v_opt=false

while getopts :vf: opt; do
    case $opt in 
        f) find_out=$(find . -name "$OPTARG") 
           # do something with $find_out, such as checking if it's empty, or
           # if more than one file was found ...
           [[ $find_out ]] && filename=$(head -n 1 <<< "$find_out")
           ;;
        v) v_opt=true ;;
        ?) echo "usage: ..."; exit 1;;
    esac
done
shift $((OPTIND-1))

echo $filename
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.