0

I created a bash script that will print qmb00l if the sch is 4cn.tbl, and qmb00l45 if the sch is 4cn1.tbl, but when I execute the script, there is no response in terminal. I want to print like this

Sch Name : qmb00l

cd /home/fj/vanessa/Desktop
sch="$(echo -n " Sch Name  : "; grep -o '4cn.tbl\|4cn1.tbl' schmaneg.sch)"
                        if [ "$sch" = 4cn.tbl ]; then 
                            sch = "qmb00l"
                            echo $sch
                        fi
                        if [ "$sch" = 4cn1.tbl ]; then
                            sch = "qmb00l45"
                            echo $sch
                        fi

this is what inside the schmaneg file:

blah
4cn.tbl
bleh

I debugged but nothing happened.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Here is what inside the schmaneg file: – vane0121.vb Nov 09 '22 at 17:14
  • Please, do not use pictures. I think you want `[ "$sch" = '4cn.tbl' ]` instead of `[ "$sch" = 4cn.tbl ]` – David Ranieri Nov 09 '22 at 17:16
  • Note that assignments don't work when you have spaces around the `=` – Charles Duffy Nov 09 '22 at 17:29
  • [Why does a space in a variable assignment give an error in bash?](https://stackoverflow.com/questions/41748466/why-does-a-space-in-a-variable-assignment-give-an-error-in-bash) – Charles Duffy Nov 09 '22 at 17:30
  • Also, please provide code, errors, execution transcripts, &c. **as text**. See [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/a/285557/14122) on [meta]. – Charles Duffy Nov 09 '22 at 17:30
  • Running `bash -x yourscript` is also a good place to start in trying to figure out where in the execution of `yourscript` things first don't go according to plan. – Charles Duffy Nov 09 '22 at 17:31
  • 2
    That said, you're putting the string `Sch Name` inside your variable, and comparing it against values that _don't_ contain that string; so _of course_ it'll never match. – Charles Duffy Nov 09 '22 at 17:32
  • Also, note that `sh` and `bash` are two different shells. If you want to run your script with bash, only use `bash yourscript`, never `sh yourscript`. – Charles Duffy Nov 09 '22 at 17:33
  • Anyhow -- I'd suggest keeping the `Sch Name:` label outside of your variable's contents, and adding it only when you echo the value. – Charles Duffy Nov 09 '22 at 17:41
  • @DavidRanieri, since none of the characters in `4cn.tbl` are subject to any kind of shell parsing when used unquoted, what difference would it make? – Charles Duffy Nov 09 '22 at 17:42
  • Hi already removed the spaces between the = sign and the output is close to what I want. But how can I get this output? Sch Name : qmb00l – vane0121.vb Nov 09 '22 at 17:46
  • this is the revised code but still I don't get what I want. cd /home/fj/vanessa/Desktop sch="$(echo -n " Sch Name : "; grep -o '4cn.tbl\|4cn1.tbl' schmaneg.sch)" if [ "$sch"=4cn.tbl ]; then sch = "qmb00l" echo $sch fi if [ "$sch"=4cn1.tbl ]; then sch="qmb00l45" echo $sch fi – vane0121.vb Nov 09 '22 at 17:49
  • @vane0121.vb With this code, `$sch` will never be either "4cn.tbl" or "4cn1.tbl". Instead, it will be " Sch Name : 4cn.tbl" or " Sch Name : 4cn1.tbl", and these are not the same as what you are testing for. As Charles Duffy suggested, it'd be better not to include the laben in the variable's value. – Gordon Davisson Nov 09 '22 at 18:08

1 Answers1

2

After you run this line

sch="$(echo -n " Sch Name  : "; grep -o '4cn.tbl\|4cn1.tbl' schmaneg.sch)"

$sch contains " Sch Name. : 4cn.tbl"

Then these comparisons never succeed: [ "$sch" = 4cn.tbl ] or [ "$sch" = 4cn1.tbl ]

Keep the details that are meant for output until you actually need to output them:

cd /home/fj/vanessa/Desktop

case "$(grep -o '4cn.tbl\|4cn1.tbl' schmaneg.sch)" in
    "4cn.tbl")  sch=qmb00l    ;;
    "4cn1.tbl") sch=qmb00l45  ;;
    *)          sch="unknown" ;;
esac

echo " Sch Name  : $sch"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352