LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-08-2012, 01:51 PM   #16
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928

Quote:
Originally Posted by Cedrik View Post
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
Of course it is ... and so is =~ (coincidentally described in the same bit
of bash's man-page that you quoted), which you claimed, by way of different example
Quote:
BTW, a pattern matching looks like:
wasn't pattern matching.


Cheers,
Tink
 
Old 02-08-2012, 01:56 PM   #17
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Ok, I admit I wasn't clear
 
Old 02-12-2012, 08:25 AM   #18
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 690

Original Poster
Rep: Reputation: 34
Hello,

so far I was helped very well with problems that occurred.

Now I have a third question :
Code:
echo "before"
$StartProg=$(/sbin/service prog start)
echo $StartProg
echo "after"
gives output :
Quote:
before
bashscript.sh: line 14: =Starting: command not found
But I notice that the service "prog" has in fact started !

Why does bash report "command not found" ?
Is there something wrong with my syntax ?

When I execute /sbin/service prog start on CLI, then output is normal :
Quote:
Starting prog: [ OK ]
 
Old 02-12-2012, 08:49 AM   #19
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
When bash processes $StartProg=$(/sbin/service prog start), it:
  1. Runs /sbin/service prog start (starting the service) and substitutes its output into the line leaving $StartProg=Starting <whatever>
  2. Substitutes the value of $StartProg which is nothing, leaving =Starting <whatever>
  3. Runs that as a command, resulting in the error message you see.

Last edited by catkin; 02-12-2012 at 08:50 AM. Reason: speeling and fix missing markup
 
Old 02-12-2012, 09:55 AM   #20
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 690

Original Poster
Rep: Reputation: 34
But I read all over the internet that the syntax $(command) is the correct way of executing a command.

How do I then get normal output of the command so that the rest of the bashscript can continue ?
 
Old 02-12-2012, 12:05 PM   #21
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by jonaskellens
But I read all over the internet that the syntax $(command) is the correct way of executing a command.
It is, but the problem is not with the $( ... ) construct.

Re-read point #2 in catkin's message.

In bash, when you want to use the value of a variable, you prefix the variable name with a '$'.
Code:
echo "This is the value: $variableValue"
When you want to assign a value to a variable, you do not prefix the variable name with a '$'.
Code:
variableValue="This is the assigned value"
This is different from other languages (e.g. PHP) where the '$' prefixes every variable reference (assignment or substitution).

Last edited by Dark_Helmet; 02-12-2012 at 12:08 PM.
 
Old 02-12-2012, 01:19 PM   #22
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 690

Original Poster
Rep: Reputation: 34
OK I see. I did not notice this before. In the rest of my script I did not make this mistake.

The error-message "=Starting: command not found" is gone, that's good !

However, even with StartProg=$(/sbin/service prog start) I still get no output and the script is not further executed.
 
Old 02-13-2012, 01:46 AM   #23
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jonaskellens View Post
But I read all over the internet that the syntax $(command) is the correct way of executing a command.
It is the correct way to capture the output of a command when modified to my_variable=$(command 2>&1) (the 2>&1 captures the stderr output as well as the stdout).

The way to run a command is simply command

This makes sense when you think of a bash script being a automated command prompt session. At the command prompt, you run a command by typing it and pressing Enter.
 
Old 02-13-2012, 01:49 AM   #24
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jonaskellens View Post
However, even with StartProg=$(/sbin/service prog start) I still get no output and the script is not further executed.
As long as the command terminates you should see the output of
Code:
echo $StartProg
echo "after"
Please post your latest script if you do not see that output.
 
Old 02-18-2012, 04:58 AM   #25
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 690

Original Poster
Rep: Reputation: 34
Thank you for your help. I notice that execution of the command hangs... I think it is related to the service that is being started. That's why I now execute another bash script which executes this command. This way my "main" bash script is further executed. It works well.

Another question :

suppose CheckNumber="[space][space][space]7"

I want to get the last 2 ciphers of this string. If there is only one cipher, I want to delete white spaces. I do the following :

Number=${CheckNumber:0:2} # get last 2 characters
${Number//[[:space:]]} # delete white spaces

if [[ "$Number"=="0" ]]; then
echo "number 0"
else
echo "number more than 0"
fi

I notice that the variable $Number is always empty.
So my if-then-else also fails.

See what I'm doing wrong ?

Last edited by jonaskellens; 02-18-2012 at 04:59 AM.
 
Old 02-18-2012, 05:04 AM   #26
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If the command does not terminate you could put it in the background by appending & to the command.
 
Old 02-18-2012, 05:09 AM   #27
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by jonaskellens View Post
suppose CheckNumber="[space][space][space]7"

I want to get the last 2 ciphers of this string. If there is only one cipher, I want to delete white spaces. I do the following :
Code:
Number=${CheckNumber:0:2} # get last 2 characters
${Number//[[:space:]]} # delete white spaces

if [[ "$Number"=="0" ]]; then
	echo "number 0"
else 
	echo "number more than 0"
fi
I notice that the variable $Number is always empty.
So my if-then-else also fails.

See what I'm doing wrong ?
Add the command set -xv just before the problem section to better see what the script is doing.

Bash wants spaces either side of comparison operators such as == and you want the numeric comparison operator -eq, not the string comparison operator ==.
 
Old 02-18-2012, 05:53 AM   #28
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 690

Original Poster
Rep: Reputation: 34
Code:
CheckNumber="   7"
+ CheckNumber='   7'
echo $CheckNumber
+ echo 7
7
Number=${CheckNumber:0:2}
+ Number='  '
echo "   Number: $Number"
+ echo '   Number:   '
   Number:   
${Number//[[:space:]]}
echo "   Number: $Number"
+ echo '   Number:   '
   Number:   
if [[ "$Number"=="0" ]]; then
	echo "number 0"
else 
	echo "number more than 0"
fi
+ [[ -n   ==0 ]]
+ echo 'number 0'
number 0
 
Old 02-18-2012, 06:06 AM   #29
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The comment does not match the code in ...
Code:
Number=${CheckNumber:0:2} # get last 2 characters
... the code gets the first two characters as the set -xv trace shows.

If you want to use echo to display the value of variables you must double quote them or bash will strip leading/training whitespace and convert all embedded whitespace sequences to a single space. It is also helpful to put markers at beginning and end so you can see any leading/training whitespace:
Code:
echo "Number: '$Number'"
The else echo in ...
Code:
if [[ "$Number"=="0" ]]; then
	echo "number 0"
else 
	echo "number more than 0"
fi
... is wrong. The else case is triggered when $Number is not equal to the string 0 -- or would be if the == in the test expression had space either side of it so it was a comparison operator and not a component of the string "$Number"=="0" which is actually "==0" at run time as the set -xv trace shows.

Last edited by catkin; 02-18-2012 at 06:08 AM.
 
Old 02-18-2012, 06:40 AM   #30
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 690

Original Poster
Rep: Reputation: 34
${CheckNumber: -2} does now what I want it to do...

Number=${Number/ /} removes white spaces...

However, still looking for a correct if-then-else

Last edited by jonaskellens; 02-18-2012 at 06:55 AM.
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:17 AM.

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