Help with bash script problem

Hi,

Below is my bash script:

[perl_beginner@]cat run_all.sh

if [ $1 !~/+/ ] && [ $2 !~/+/ ]; then
Number_Count_Program $1.results $2.results > $1.$2.counts
else
Number_Split_Program $1.results $2.results > $1.$2.split
fi

After I run the following command:

./run_all.sh A B
./run_all.sh: line 1: [: A: unary operator expected

Anybody advice to edit my bash script, run_all.sh ?

The purpose I write the bash script is automatic the server run "Number_Count_Program" if first and second argument key in is don't have any "+";
As long as either first or second argument got "+", it will run "Number_Split_Program".

"Number_Count_Program" and "Number_Split_Program" is an c++ program which worked well.
Thanks for any advice.

if [[ $1 =~ \+ ]] && [[ $2 =~ \+ ]]; then
 echo "got two +'s"
else
 echo "didn't get two +'s"
fi

You can use ! before the tests to negate if you must have it the other way around.

Try something like the following:

#!/bin/bash

if [[ $1 =~ "+"  &&  $2 =~ "+" ]]
then
     echo "Match"
else
     echo "No Match"
fi
1 Like

Hi Scott,

I just try your code edit.
But it seems like the script will run "Number_Count_Program" if I key in something like "./run_all.sh A+B B+C"?

What I prefer is "Number_Count_Program" is run only when both first and second argument don't have any "+".
If either first or second or both got "+", I will prefer it run "Number_Split_Program".

Kindly correct me if I was wrong.
Thanks.

You will have to negate the tests, or swap around the code in your if "then" and "else" blocks.

1 Like

The + is special in an ERE.
I found it is necessary to quote and escape the +

if [[ $1 =~ "\+" && $2 =~ "\+" ]]; then