Error testing a bash script

wallis

New Member
Joined
May 15, 2024
Messages
4
Reaction score
1
Credits
52
the following is the text of my bash script I named script.txt:
#!/bin/sh
direction=${1^^}
filename=$2
system=${3^^}
echo "system = $system"
echo "direction = $direction"
#test for direction = "TX" or "RX"
if [ $direction != "TX" ] & [ $direction != "RX" ]; then
echo "Error - First argument must be 'TX' or 'RX' "
exit 1
fi
#end of script

I enter "bash script tx filename c" in a terminal prompt
The script output is:
system = C
direction = TX
Error - First argument must be 'TX or 'RX'

What is wrong with my if statement? $direction is = "TX"
I also attached my script.txt file
 

Attachments

  • script.txt
    281 bytes · Views: 1,217


It looks like your if statement needs a && instead of a single &.

if [ "$direction" != "TX" ] && [ "$direction" != "RX" ]; then
 
the following is the text of my bash script I named script.txt:
#!/bin/sh
direction=${1^^}
filename=$2
system=${3^^}
echo "system = $system"
echo "direction = $direction"
#test for direction = "TX" or "RX"
if [ $direction != "TX" ] & [ $direction != "RX" ]; then
echo "Error - First argument must be 'TX' or 'RX' "
exit 1
fi
#end of script

I enter "bash script tx filename c" in a terminal prompt
The script output is:
system = C
direction = TX
Error - First argument must be 'TX or 'RX'

What is wrong with my if statement? $direction is = "TX"
I also attached my script.txt file

The problem lies within the logical condition [ $direction != "TX" ] & [ $direction != "RX" ].


The correct operator for combining conditions is '&&' (logical AND) instead of '&' (background process). Using a single ampersand ('&') creates a background process, and the second condition is not evaluated as you would expect in this case.


#!/bin/sh
direction=${1^^}
filename=$2
system=${3^^}
echo "system = $system"
echo "direction = $direction"
#test for direction = "TX" or "RX"
if [ "$direction" != "TX" ] && [ "$direction" != "RX" ]; then
echo "Error - First argument must be 'TX' or 'RX'"
exit 1
fi



Now, when you run the command bash script.txt TX filename c, it should execute as expected without any errors.
 
Moving this to Command Line where scripting help is provided.

Wizard
 
Thanks for your reply, the && did the trick. I guess I should also use || for an or comparison instead of just one |
 
Thanks for your reply, the && did the trick. I guess I should also use || for an or comparison instead of just one |
In shell scripting, || and | serve different purposes, so they can't be used interchangeably.

|| is used for logical OR in conditionals, typically in if statements or command chaining. It's used to execute a command or series of commands only if the preceding command fails (returns a non-zero exit status).

| is used for piping output from one command to another. It takes the standard output (stdout) of the command on its left side and feeds it as standard input (stdin) to the command on its right side.
In your script, you're using [ ] for conditional testing, and || is the correct operator for logical OR within if statements. So, you should keep using || as you have it:


bash

if [ "$direction" != "TX" ] || [ "$direction" != "RX" ]; then
echo "Error - First argument must be 'TX' or 'RX'"
exit 1
fi

This checks if the value of $direction is not equal to either "TX" or "RX" and prints an error message if that's the case.

If I'm understanding the intent properly.
 
Insert this somewhere at the start of the program:

set -x

In shell scripting, || and | serve different purposes, so they can't be used interchangeably.

That's euhm, an understatement :)

So you basically say that different functions do different things
 
Thanks for your reply, the && did the trick. I guess I should also use || for an or comparison instead of just one |

The & indeed is for background processing, and it typically is set at the end of the command. Often used with nohup, like this:

nohup myscript.sh some parameters &
 

Members online


Top