Register
It is currently Tue Oct 21, 2014 1:57 pm

if statement


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Nov 10, 2009 6:36 am   

Joined: Tue Nov 10, 2009 6:27 am
Posts: 2
Hi everybody,
I am a beginner with bashing and need ur help with a small bash program. what i need is to append "inkognito@" to the beginning of every line in a file. this worked. Then i needed to insert a new line after that 30 lines are read and appended and this is where i started to have problems. i am getting an error message telling me that [blabla: isnt a command. the error is in the if statement condition. the code is as follows:
Code:
#!/bin/bash
NUMBER=0;
CONST=30;
cat /home/haidar/Desktop/all\ nodes.hosts | while read LINE ; do
printf "inkognito@$LINE ";
NUMBER=$(($NUMBER+1));
if [$NUMBER -eq $CONST];then
$NUMBER=0;
echo" ";
fi
done


Top
 Profile  
 PostPosted: Tue Nov 10, 2009 7:22 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Code:
#!/bin/bash

while read LINE ; do
   echo "inkognito@$LINE "
   [ $((NUMBER++ % 30)) -eq 0 ] && echo
done < '/home/haidar/Desktop/all nodes.hosts' > '/home/haidar/Desktop/all nodes.hosts_modified'

don't forget spaces after and before square brackets.


Last edited by Watael on Tue Nov 10, 2009 9:30 am, edited 2 times in total.

Top
 Profile  
 PostPosted: Tue Nov 10, 2009 8:17 am   

Joined: Tue Nov 10, 2009 6:27 am
Posts: 2
thanx, that solved the prob


Top
 Profile  
 PostPosted: Tue Nov 10, 2009 8:40 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
inkognito wrote:
thanx, that solved the prob


Just so you know why
Code:
if [$NUMBER -eq $CONST];then


Bash does not like it when you do [$variable, it must be [ $variable... note the space added between the bracket and $variable, this also applies to the end, $variable ] is the correct syntax.

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP