Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement not playing ball in bash 1

Status
Not open for further replies.

3wsparky

IS-IT--Management
May 25, 2003
121
GB
COUNT= nmap 172.30.167.74 | grep -c 23
if [ $COUNT ="1" ]; then
echo "Action A"
else
echo "Action B"
fi

output if matched with the grep or not always picks the same statment friend mentioned this to be the cure but i am unable to work this one out aswell

COUNT=`yourcmd | grep -c Cisco'
if [ $COUNT -ge 1 ]; then
echo "Action A"
else
echo "Action B"
fi

where am i going wrong
 
And what about this ?
COUNT=$(nmap 172.30.167.74 | grep -c 23)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
well that must be the fasted post ever resolved :)
 
one question tho

if action B is the outcome can i use some sort of a goto statement ?
 
Hi,

There is no GOTO in shell.
You can organize your statements as functions and call them using if then else.
 
i have now setup a function for this but it fails as follows

function portscan () {
echo port

COUNT=$(nmap $network.$begin | grep -c 23)
if [ $COUNT -ge 1 ]; then
backup
}


pingback: line 69: syntax error near unexpected token `}'
pingback: line 69: `}'
deb:/var/lib/tftpboot#

if i comment out the >if [ $COUNT -ge 1 ]; then< then it runs ok but of course is not running as requred.

whats wrong with this it runs ok not as a function !

 
In you first post you had it:
A if has to be ended by a fi
 
function portscan () {
echo port

COUNT=$(nmap $network.$begin | grep -c 23)
if [ $COUNT -ge 1 ]; then
backup
else
echo it ain't going to backup
fi
}

pingback: line 43: syntax error near unexpected token `else'
pingback: line 43: `else'
deb:/var/lib/tftpboot#

the else is from the statement above what is the problem with that ?
 
Hi

Maybe the single single quote ( ' ) requires its pair... Or just has to be quoted or escaped.
Code:
    [red]here --v[/red]
echo it ain[red]'[/red]t going to backup



echo [red]"[/red]it ain't going to backup[red]"[/red]

[gray]# or[/gray]

echo it ain[red]\[/red]'t going to backup

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top