LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-06-2012, 02:47 PM   #1
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Rep: Reputation: 33
Bash script gives weird output


Hello,

when executing the following bash script :

Code:
echo "1"
CheckMainServer=$(/usr/bin/sipsak -vv -s sip:yo7@${SIPserver} -c sip:yo7@${SIPserver} --password 2aHOGrI)
echo "2"
echo $CheckMainServer
echo "3"
The output on CLI is as followed :

Quote:
[root@jonas Desktop]# sh bashscript.sh
1
2
3211988943~ 3211988947~ 952.pdf app_voicemail.c~ b2evolution bashscript.sh BusinessCard decl2009.pdf Deploying_OpenLDAP.pdf ask extension Fax faxrates.sql~ sound.jpg test2.csv test2.csv~ test.csv test.csv~ testmailing.svg test.ods test.pdf tmp_ldap~ Unreachable.txt Webmin-htb WhiteandOrangeBlogTheme SIP/2.0 404 Not Found final received
The last "SIP/2.0 404 Not Found final received" is a part of the output that I expect... but all the rest ?!

Why are my Desktop files displayed ?
 
Old 02-06-2012, 03:03 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I don't know ... but what does
Code:
/usr/bin/sipsak -vv -s sip:yo7@${SIPserver} -c sip:yo7@${SIPserver} --password 2aHOGrI
all by itself tell you?



Cheers,
Tink
 
Old 02-06-2012, 03:13 PM   #3
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Tinkster View Post
I don't know ... but what does
Code:
/usr/bin/sipsak -vv -s sip:yo7@${SIPserver} -c sip:yo7@${SIPserver} --password 2aHOGrI
all by itself tell you?
Executing this command on CLI just gives the desired output. Nothing more.

When executing this in bash script, it gives all the weird Desktop-information...
 
Old 02-06-2012, 03:16 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
You missed Tinkster's point

He wanted you to paste the exact output of the command.

It's likely that your command's output includes an asterisk. Hence, when you:
Code:
echo $CheckMainServer
the shell will expand the asterisk as a filename wildcard--which results in a list of your files in the current directory.

It may not be an asterisk, but some other character that is interpreted by the shell. I just used the asterisk as an example.

EDIT:
You can avoid some bash substitution problems by quoting variables. For instance:
Code:
echo "$CheckMainServer"

Last edited by Dark_Helmet; 02-06-2012 at 03:20 PM.
 
Old 02-06-2012, 03:19 PM   #5
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Rep: Reputation: 8
is this your hole script file? where is the shebang?
and please do tell what are you trying to do with this script.
tanX
 
Old 02-06-2012, 03:23 PM   #6
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Dark_Helmet View Post
EDIT:
You can avoid some bash substitution problems by quoting variables. For instance:
Code:
echo "$CheckMainServer"
Problem solved by using : echo "$CheckMainServer"

Thanks !
 
Old 02-08-2012, 04:16 AM   #7
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
I have another question :

CheckMainServerOutput = "final received"

When executing this :
Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then
I get the following notice :
Quote:
bashscript.sh: line 15: [: =~: binary operator expected
bashscript.sh: line 15: [: =~: binary operator expected
So what is the correct syntax ?
 
Old 02-08-2012, 04:55 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by jonaskellens View Post
So what is the correct syntax ?
Remove the double quotes around the regular expression?
 
Old 02-08-2012, 05:30 AM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Code:
# .  : match any character
# *  : previous char is present zero or more times
# \. : match a dot

if [[ "$CheckMainServerOutput" =~ .*ms.* ]] || [[ "$CheckMainServerOutput" =~ .*32\.43\.26.* ]]; then
BTW, a pattern matching looks like:
Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then

Last edited by Cedrik; 02-08-2012 at 05:52 AM.
 
1 members found this post helpful.
Old 02-08-2012, 11:15 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Cedrik View Post
Code:
# .  : match any character
# *  : previous char is present zero or more times
# \. : match a dot

if [[ "$CheckMainServerOutput" =~ .*ms.* ]] || [[ "$CheckMainServerOutput" =~ .*32\.43\.26.* ]]; then
BTW, a pattern matching looks like:
Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then

No, it doesn't. Pattern matching uses =~, and uses an actual
regex-engine, so to match a literal period .*32\.43\.26.* is the
correct form.


Try this:
Code:
if [[ "$CheckMainServerOutput" =~ *ms*  ||  "$CheckMainServerOutput" =~ *32.43.26* ]]; then
Cheers,
Tink
 
Old 02-08-2012, 12:18 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Tinkster View Post
Code:
if [[ "$CheckMainServerOutput" =~ *ms*  ||  "$CheckMainServerOutput" =~ *32.43.26* ]]; then
Is that what you intended, Tinkster? AFAIK it means "$CheckMainServerOutput" contains *m followed by any number of the character s or *32 followed by any character ...

On further reflection, the reason bash generated syntax errors on interpreting
Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then
is that the =~ comparison operator is not valid in the [ ... ] test form, only in the [[ ... ]] form.
 
Old 02-08-2012, 12:51 PM   #12
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Quote:
Originally Posted by Tinkster View Post
No, it doesn't. Pattern matching uses =~, and uses an actual
regex-engine, so to match a literal period .*32\.43\.26.* is the
correct form.


Try this:
Code:
if [[ "$CheckMainServerOutput" =~ *ms*  ||  "$CheckMainServerOutput" =~ *32.43.26* ]]; then
Cheers,
Tink
Maybe I used the wrong term ("pattern") but:
Code:
[[ "linux" == *in* ]] && echo ok
ok
[[ "linux" =~ *in* ]] && echo ok
<nothing>
 
Old 02-08-2012, 01:35 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by catkin View Post
Is that what you intended, Tinkster? AFAIK it means "$CheckMainServerOutput" contains *m followed by any number of the character s or *32 followed by any character ...
My intention was to quote the string the OP used. I think I succeeded.
Whether the original string is what the OP really wanted I don't know.

Quote:
Originally Posted by catkin View Post
On further reflection, the reason bash generated syntax errors on interpreting
Code:
if [ "$CheckMainServerOutput" =~ "*ms*" ] || [ "$CheckMainServerOutput" =~ "*32.43.26*" ]; then
is that the =~ comparison operator is not valid in the [ ... ] test form, only in the [[ ... ]] form.
Well spotted. That's what I changed above.


Cheers,
Tink
 
Old 02-08-2012, 01:37 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Cedrik View Post
Maybe I used the wrong term ("pattern") but:
Code:
[[ "linux" == *in* ]] && echo ok
ok
[[ "linux" =~ *in* ]] && echo ok
<nothing>
Remove the leading aster; it's not a valid regex.
 
Old 02-08-2012, 01:45 PM   #15
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
man bash:
Code:
...
 [[ expression ]]
...
              When the == and != operators are used, the string to  the  right
              of the operator is considered a pattern and matched according to
              the rules described below under Pattern Matching.  If the  shell
              option  nocasematch  is  enabled, the match is performed without
              regard to the case of alphabetic characters.  The  return  value
              is  0 if the string matches (==) or does not match (!=) the pat-
              tern, and 1 otherwise.  Any part of the pattern may be quoted to
              force it to be matched as a string.
              ...
              An  additional  binary operator, =~, is available, with the same
              precedence as == and !=.  When it is used,  the  string  to  the
              right  of the operator is considered an extended regular expres-
              sion and matched accordingly (as in regex(3)).  The return value
              is 0 if the string matches the pattern, and 1 otherwise.
...
       Pattern Matching

       Any character that appears in a pattern, other than the special pattern
       characters described below, matches itself.  The NUL character may  not
       occur  in  a pattern.  A backslash escapes the following character; the
       escaping backslash is discarded when  matching.   The  special  pattern
       characters must be quoted if they are to be matched literally.
              
       The special pattern characters have the following meanings:
              
       *      Matches  any  string, including the null string.
...
So I was right, this is pattern matching:
Code:
if [[ "$CheckMainServerOutput" == *ms* ]] || [[ "$CheckMainServerOutput" == *32.43.26* ]]; then

Last edited by Cedrik; 02-08-2012 at 01:46 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
diff the output of one bash script against another Finejason Linux - General 3 07-07-2011 03:41 AM
Bash Script Missing Output Slick666 Programming 1 10-05-2007 08:09 AM
Weird Nautilus Bash Script Problem fatsheep Programming 2 11-19-2006 05:51 PM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 10:26 PM
bash script connect from one host to another and to another weird aim nakkaya Linux - Networking 1 06-30-2003 02:28 PM


All times are GMT -5. The time now is 04:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration