1

Having trouble understanding why my bash script terminates abrubtly

echo "Checking for ANY QUERY"
dig +short $MYHOSTWITHOUTWWW  any | tr '[A-Z]' '[a-z]'  > $HOME/$MYHOST-$MYTIMESTAMP/$MYHOST-$MYTIMESTAMP-anyquery.txt
ANYQUERY_STATUS=`grep -iwo 'disabled' $HOME/$MYHOST-$MYTIMESTAMP/$MYHOST-$MYTIMESTAMP-anyquery.txt`

if [[ "$ANYQUERY_STATUS" = "disabled"  ]]; 
then
    echo "disabled" > $HOME/$MYHOST-$MYTIMESTAMP/$MYHOST-$MYTIMESTAMP-anyquerybug.txt
else
    echo "enabled" > $HOME/$MYHOST-$MYTIMESTAMP/$MYHOST-$MYTIMESTAMP-anyquerybug.txt
fi

echo "Checking for Generic top level domains availabilty"
touch  $HOME/$MYHOST-$MYTIMESTAMP/$MYHOST-$MYTIMESTAMP-domain-gtld.txt

However, the program stops and exits at the 'ANYQUERY_STATUS=`grep -iwo 'disabled' statement. It does not follow through executing the rest of the script.

Any help will be highly appreciated here.

Magnus Melwin
  • 1,509
  • 1
  • 21
  • 32
  • How are you running the script? Is this the whole script? No `set -e` or `bash -e`? – tripleee Jul 13 '18 at 07:58
  • The capturing of all output into files and further into variables is clumsy and unidiomatic. Do you really require for all these files to be created, or is ths because you aren't very familiar with shell scripting yet? – tripleee Jul 13 '18 at 08:00
  • 1
    Try to use double quotation mark embrace the path for `grep`. And did any error message reveal? – CWLiu Jul 13 '18 at 08:01
  • Its part of the script, tried the double quotes too...but it returns with + ANYQUERY_STATUS= and exits out. The capturing of output into files and vars is a requirement of the project. not much i can do about that. – Magnus Melwin Jul 13 '18 at 08:05
  • I have tried replacing the string comparison with exit code comparison - if [[ "$?" -eq 0 ]]; still no luck it doesnt reach this statement. terminates at the grep statement – Magnus Melwin Jul 13 '18 at 08:16
  • Have you tested that `grep` statement by itself? What did it output? – Iguananaut Jul 13 '18 at 08:22
  • Can you do `echo $-` before the `ENTQUERY_STATUS` line and post the output? Btw, it's easier to just write `if grep -qiwo disabled ; then ....` – KamilCuk Jul 13 '18 at 08:22
  • Run the script like ksh -x ./your_Script.sh and verify where exactly it is failing – Abhijit Pritam Dutta Jul 13 '18 at 08:39
  • Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jul 13 '18 at 09:40
  • With long file names, don't repeat yourself. Store that string in a variable (do not call it PATH). – William Pursell Jul 13 '18 at 14:51
  • Also, try `if ! grep -iow disabled "$input_file"; then echo enabled; fi > "$output_file"` – William Pursell Jul 13 '18 at 14:53

1 Answers1

2

The grep command is returning exit status 1 to indicate when no lines matched and this is causing the variable assignment to fail. It is better to execute the command directly in the script and then use $?.

Change the logic of your script so that you can use the status directly:

grep -iwo 'disabled' $HOME/$MYHOST-$MYTIMESTAMP/$MYHOST-$MYTIMESTAMP-anyquery.txt
if [[ $? = 0 ]];
Nick
  • 4,820
  • 18
  • 31
  • 47
  • `if` by definition runs a command and examines its exit code to decide whether to take the `then` branch or the `else` branch. Anything that looks like `command; if [ $? = 0 ]` is better written `if command` and you should generally almost never need to examine `$?` directly. – tripleee Jul 13 '18 at 09:10
  • Ah yes, makes sense. Ty triplee for the input. – Magnus Melwin Jul 13 '18 at 09:50
  • Making code more readable by separating as I did shouldn’t be discounted. – Nick Jul 13 '18 at 09:51