0

My current Bash script looks like in the following. So far it's working except I have no idea how to make it so the two options -f and -o work together:

  • the -o FILE option to display the most relevant information in a file passed as an argument (if the file does not exist an error message must be displayed)
  • the option -f KEYWORD to display the lines containing the KEYWORD to from a file. This option must be used with the -o option

Thank you for any input

    #!/bin/bash
    function s_func()
{

filename="$1";

echo "when you're done saving information please write 'exit'"  
        script $filename.txt

}
function o_func()
{

filename="$1"
dest='/home/eya/'

if [ -f "$filename".txt ]; then
cat $filename.txt
else echo "file does not exist"
fi
}

function f_func()
{
keyword="$1"
filename="$2"
grep $keyword $filename.txt
}
    while getopts ":s:o:f:" opt; do
        case $opt in 
            s) s_func  "$OPTARG";;
            o) o_func  "$OPTARG";;
            f) f_func  "$OPTARG";;
            \?)echo "wrong option";exit 1;;
        esac
        done
    shift $((OPTIND -1))
Thomas Hansen
  • 775
  • 1
  • 6
  • 17
eya
  • 3
  • 3
  • 1
    Haven't you posted this code (I wouldn't say "asked this question", because this is more of a code dump than a question in its current state) before in the past? – Charles Duffy Mar 26 '21 at 19:50
  • Please try to make your code into a [mre] -- the _shortest possible thing_ that demonstrates a _specific problem_ when run without changes. (Both expected and actual output, likewise, should be shown in the question itself). – Charles Duffy Mar 26 '21 at 19:52
  • BTW, note how you're using `filename` in one place, and `file` in the other? If you make `f_func` write to a global variable (and all variables are global in bash by default), and `s_func` _read from_ that global variable... well, there you are (as long as they're used in the intended order). – Charles Duffy Mar 26 '21 at 19:53

1 Answers1

0

Try this: while processing commnand line options, just collect variables. Only do things with those variables after the options are parsed.

declare f_arg o_arg s_arg

while getopts ":s:o:f:" opt; do
    case $opt in 
        s) s_arg=$OPTARG ;;
        o) o_arg=$OPTARG ;;
        f) f_arg=$OPTARG ;;
    esac
done
shift $((OPTIND -1))

if [[ -z $o_arg ]] || [[ -z $f_arg ]] || [[ -z $s_arg ]]; then
    echo "ERROR: Options -s, -o and -f are required." >&2
    exit 1
fi

# Now you can do stuff in a specific order.
o_func "$o_arg"
f_func "$f_arg"
s_func "$s_arg"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • thank you so much but I have a small problem how do I make it so the -f option only does the search with the keyword while the -o option is the one that does the displaying cause right now when I use them together it displays the whole file and then it displays what I searched for – eya Mar 26 '21 at 20:36
  • I don't understand what you mean. Is doing `cat $filename` in the "o" func a problem? – glenn jackman Mar 26 '21 at 21:33
  • the output of ./ex.sh -f keyword -o file should only show the lines with the keyword however when now it displays the whole document then the lines with the keyword "the option -f KEYWORD to display the lines containing the KEYWORD to from a file. This option must be used with the -o option" thank you again for your time – eya Mar 26 '21 at 21:40
  • Well, that behaviour is up to you, the programmer. I don't know what you're asking me to do for you. – glenn jackman Mar 26 '21 at 23:21