0

Below is the list of files i've in my current directory:

ctc.SNAPSHOT-1.0.1-abc.gz
ctc.SNAPSHOT-2.0.1-bcd.gz
ApprovedList.A
ApprovedList.B

cat ApprovedList.A
BC
DEF

I want to pass 3 parameters to the script,but i guess the script is unable to read the variable : $num1.

Below is my code:

#!/bin/bash

case "$#" in
    0|1|2)
        echo "Correct Usage: $0 A/B/C <rec_name>/ALL <version_number>."
        exit 1
        ;;
esac

ops () {
    echo $line
}

num1=$(for i in *gz; do num=$(echo $i|sed -E -n 's/ctc.*\-(.*)\-.*/\1/p'); echo $num; done)

while read -r line
do
    case "$1" in
        A|B|C)case "$2" in
            $line|ALL)case "$3" in
            $num1)ops;;
        esac;;
esac;;
esac
done < /home/us3r/ApprovedList."$1"

When i execute the script, it gives no output.

Execution: ./script.sh A ALL 1.0.1

Expected output:

BC
DEF

I have tried the above code using multiple if conditions and it's working, i just wanted to make my code more readable and compact ,so i thought of using case statements.

1 Answer 1

1

Below is the list of files i've in my current directory:

ctc.SNAPSHOT-1.0.1-abc.gz
ctc.SNAPSHOT-2.0.1-bcd.gz

num1=$(for i in *gz; do num=$(echo $i|sed -E -n 's/ctc.*\-(.*)\-.*/\1/p'); echo $num; done)

This will set num1 to 1.0.1 2.0.1

Execution: ./script.sh A ALL 1.0.1

$line|ALL)case "$3" in
            $num1)ops;;
        esac;;

And this will not match it, since $3 is 1.0.1

3
  • Okay, any way to make that correct?
    – us3r
    Jun 8, 2019 at 11:52
  • make it correct? you'll have to rewrite the whole script into something much more simple. Try reversing the $num1 and $3 though: ...ALL) case $num1 in *"$3"*)ops;; (don't omit the *s!).
    – user313992
    Jun 8, 2019 at 11:57
  • Thanks :). i tried as suggested by you, it worked...
    – us3r
    Jun 8, 2019 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .