LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script gives weird output (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-gives-weird-output-927936/)

jonaskellens 02-06-2012 02:47 PM

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 ?

Tinkster 02-06-2012 03:03 PM

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

jonaskellens 02-06-2012 03:13 PM

Quote:

Originally Posted by Tinkster (Post 4595303)
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...

Dark_Helmet 02-06-2012 03:16 PM

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"

devilboy09 02-06-2012 03:19 PM

is this your hole script file? where is the shebang?
and please do tell what are you trying to do with this script.
tanX

jonaskellens 02-06-2012 03:23 PM

Quote:

Originally Posted by Dark_Helmet (Post 4595317)
EDIT:
You can avoid some bash substitution problems by quoting variables. For instance:
Code:

echo "$CheckMainServer"

Problem solved by using : echo "$CheckMainServer"

Thanks !

jonaskellens 02-08-2012 04:16 AM

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 ?

catkin 02-08-2012 04:55 AM

Quote:

Originally Posted by jonaskellens (Post 4596870)
So what is the correct syntax ?

Remove the double quotes around the regular expression?

Cedrik 02-08-2012 05:30 AM

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

Tinkster 02-08-2012 11:15 AM

Quote:

Originally Posted by Cedrik (Post 4596914)
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

catkin 02-08-2012 12:18 PM

Quote:

Originally Posted by Tinkster (Post 4597118)
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.

Cedrik 02-08-2012 12:51 PM

Quote:

Originally Posted by Tinkster (Post 4597118)
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>


Tinkster 02-08-2012 01:35 PM

Quote:

Originally Posted by catkin (Post 4597164)
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 (Post 4597164)
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

Tinkster 02-08-2012 01:37 PM

Quote:

Originally Posted by Cedrik (Post 4597210)
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.

Cedrik 02-08-2012 01:45 PM

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


All times are GMT -5. The time now is 07:38 AM.