Hi

well I am facing some problem while trying to concatenate contents of several files, present inside a folder.

My code gives an error:

./test.sh: line 17: [: =: unary operator expected
and in similar lines

Attached is the code:

#!/bin/bash
str1="test_concatenated_RAxML_outputs.txt"
str2="r_test_concatenated_RAxML_outputs.txt"
str3="m_test_concatenated_RAxML_outputs.txt" 
str4="n_test_concatenated_RAxML_outputs.txt"
str5="h_test_concatenated_RAxML_outputs.txt"
for line in $(ls -l1 | grep ^d|awk '{print $9}');
do  
for lines in $(ls -l1 ./$line | grep ^d|awk '{print $9}');
do
cd ./$line
cd ./$lines
if [ $lines == "final_outputs" ]
then
 #textme=`find . -name test_concatenated_RAxML_outputs.txt -printnes/ ../../../texttemp/`
textme=`ls -l|grep -v '^d'|awk '{print $9}'`
if [ $textme = "test_concatenated_RAxML_outputs.txt" ]; then
cat $textme >>../../../texttemp/final_outputs/test_concatenated_RAxML_outputs.txt
elif [ $textme = $str2 ]; then
cat $textme >>../../../texttemp/final_outputs/r_test_concatenated_RAxML_outputs.txt
elif [ $textme = $str3 ];then
cat $textme >>../../../texttemp/final_outputs/m_test_concatenated_RAxML_outputs.txt
elif [ $textme = $str4 ]; then
cat $textme >>../../../texttemp/final_outputs/n_test_concatenated_RAxML_outputs.txt
elif [ $textme = $str5 ]; then
cat $textme >>../../../texttemp/final_outputs/h_test_concatenated_RAxML_outputs.txt
else 
cp -r ./ ../../../texttemp/final_outputs
fi
fi

it will be great if somebody can help.

Thanks
Aj

Recommended Answers

All 4 Replies

Uhm "==", maybe?

Uhm "==", maybe?

Hello,

A couple of things:

Try using the following to call your script (called myscript.sh) so you can watch it run:

/bin/bash -x ./myscript.sh

Your error is probably coming from the ";" at the end of the for line which terminates the for loop with the do statement.

The next issue I see is you used:

for line in $(ls -ll | grep ^d|awk '{print $9}');

instead of

for line in `/bin/ls `

If your login is set like most to return the output from "ls" in color it really throws the script off so make sure you use /bin/ls. Also you need to move one of your change directory commands and remember to return back to the parent dir when finished:

for line in `/bin/ls`
do 
cd $line 
for lines in `/bin/ls`
do
cd ./$lines
if [ $lines == "final_outputs" ]
then
 #textme=`find . -name test_concatenated_RAxML_outputs.txt -printnes/ ../../../texttemp/`
textme=`ls -l|grep -v '^d'|awk '{print $9}'`
if [ $textme = "test_concatenated_RAxML_outputs.txt" ]; then
cat $textme >>../../../texttemp/final_outputs/test_concatenated_RAxML_outputs.txt
elif [ $textme = $str2 ]; then
cat $textme >>../../../texttemp/final_outputs/r_test_concatenated_RAxML_outputs.txt
elif [ $textme = $str3 ];then
cat $textme >>../../../texttemp/final_outputs/m_test_concatenated_RAxML_outputs.txt
elif [ $textme = $str4 ]; then
cat $textme >>../../../texttemp/final_outputs/n_test_concatenated_RAxML_outputs.txt
elif [ $textme = $str5 ]; then
cat $textme >>../../../texttemp/final_outputs/h_test_concatenated_RAxML_outputs.txt
else 
cp -r ./ ../../../texttemp/final_outputs
fi
cd ..
fi

I said that sarcastically, and not because I didn't think it was part of the problem. Also, ls is not "colorised" for most people (or do you have some statistics to support this). I would like to see those "colorised" prompts on the standard Solaris installation, for instance. Also, even if this is the case, the problem is with an alias and a simple "\ls" will work just as well as /bin/ls. And, no, that semicolon does not terminate the for loop. I don't, however, see the "done"s closing the loops.

The stated problem is that if [ $x = 'something' ] will fail if $x is the empty string. Protect it with double-quotes: if [ "$x" = 'something' ] ; or (old fashioned hack) append some prefix to both sides: if [ _$x = '_something' ]

522% x=''
523% if [ $x = 'moop' ] ; then echo OK ; else echo 'what?'; fi
-bash: [: =: unary operator expected

...

524% if [ "$x" = 'moop' ] ; then echo OK ; else echo 'what?'; fi
what?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.