LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2012, 12:48 PM   #1
jmusbach
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
Question Why does this bash if statement error out?


I have the following part of a if statement:

Code:
elif [[ (("$ssh_to" = "<server address>") && -n egrep "oneclass_.*" $current_cos && -z egrep "oneclass_.*" ${avail_cos_arr[$(($cos_choice-1))]}) -o (("$ssh_to" = "<server address>") && -n egrep "secondclass_.*" $current_cos && -z egrep "secondclass_.*" ${avail_cos_arr[$(($cos_choice-1))]}) ]]; then
and it seems ok to me, except the following errors are thrown:

Code:
./change_cos.sh: line 100: expected `)'
./change_cos.sh: line 100: syntax error near `"oneclass_.*"'
Why?

Last edited by jmusbach; 10-24-2012 at 12:52 PM.
 
Old 10-24-2012, 01:47 PM   #2
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
A lot of errors occur because of mistakes before the place designated.

Also are the number of '(' and the number of ')' the same.
 
Old 10-24-2012, 04:46 PM   #3
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
I think this is really hard to debug as one line, so I split it up the best I can. Here is what I have so far. Please modify this into your script and test some more. I really can't do much more without knowing what the rest of the script does.
Code:
if [[ (("$ssh_to" = "<server_address>")) ]] &&
egrep -n "oneclass_.*" $current_cos &&
egrep -z "oneclass_.*" ${avail_cos_arr[$(( $cos_choice-1)) ]} \
-o "$ssh_to" = "<server address>" &&
egrep -n "secondclass_.*"  \
$current_cos && egrep -z "secondclass_.*" \
${avail_cos_arr[$(($cos_choice-1))]} ]]; then
echo 'It did work'  >> /dev/null
fi
What I did was break this into multiple lines, run it a couple of times and find out where you had extra parenthesis or missing parenthesis. Changed the egrep to
egrep [option] instead of [option] egrep, and ran it again. I don't know if it really works, but it doesn't syntax error out.

- Raj
 
Old 10-24-2012, 05:46 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@Rupadhya - I think you will find there is a big difference between using the bash test -z and egrep -z:
Code:
man test extract
-z STRING
    the length of STRING is zero

man grep extract
-z, --null-data
    Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option,
    this option can be used with commands like sort -z to process arbitrary file names.
However, I do agree that the process of breaking up the long single line is a good idea

@OP - Any reason why you use && for AND but use -o as opposed to || for OR?
 
1 members found this post helpful.
Old 10-24-2012, 07:56 PM   #5
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
Grail,

I agree with you that there is a big difference between the string manipulator -z and the egrep option of egrep -z. My bad for not finding that one out. It is really hard to debug multi process in one line commands. Usually, I break them apart (like above) and then if I really think I can get them to work as one command I put them together. I think in a shell script that you have in a file it really doesn't matter much. Some people like to have the commands all in one line for the ubiquitous one liner that has made *nix so famous. Try to do that on your favorite Windows flavor .

- Raj
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] if: Expression syntax error in csh if statement Mark_667 Programming 8 06-20-2012 10:47 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
error in if statement Swapna173 Linux - Newbie 3 03-18-2009 09:27 AM
label at the end of compuound statement error isuck@linux Linux - Software 3 08-31-2007 02:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration