LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-05-2010, 02:40 PM   #1
dr_0909
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Rep: Reputation: 0
I couldnt run my bash script code,I need help:(


I am really new in this bash scripting(even in ubuntu);I understand the codes but I couldnt execute mine((
(The script is about;
comparing the lines of the files
filea:1,2,3,4,5,6,7,8,9
fileb:1,4,5,4,6,7,8
this script checks the first n lines whether they are the same or not; and writes "same" or "different"
and I want to execute like:
myusername>comparing 8 filea fileb
different
myusername>comparing 4 filea fileb
same)

code iscomparing.txt)
#!/bin/bash
echo linenum
read n
echo enter files
read filea fileb
head -n $filea $fileb
while
do
if [wc -l <$filea -ge $fileb]
then
((n+=1))
echo "same"
else
echo "different"
fi
if diff $filea $fileb >/dev/null ; then
echo Same
else
echo Different
fi

I have read the answers of the problems like mine,but I couldnt make the connection with mine,it looks ok to me but it just doesnt run
I will appriciate your help thanks
 
Old 01-05-2010, 02:51 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by dr_0909 View Post
Code:
filea:1,2,3,4,5,6,7,8,9
fileb:1,4,5,4,6,7,8
Code:
myusername>comparing 8 filea fileb
different
myusername>comparing 4 filea fileb
same
Code:
#!/bin/bash
echo linenum
read n
echo enter files
read filea fileb
head -n $filea $fileb
while
do
   if [wc -l <$filea -ge $fileb]
   then
      ((n+=1))
	echo "same"
   else 
        echo "different"
    fi
if diff $filea $fileb >/dev/null ; then
  echo Same
else
  echo Different
fi
I have read the answers of the problems like mine,but I couldnt make the connection with mine,it looks ok to me but it just doesnt run
I will appriciate your help thanks
(Reformatted with code tags)

You're using head in a way it can't be used, your if statement isn't executing the commands (no ` or $() ) your second if isn't a valid structure and generally a while loop with a counter is a for loop. Also what are you doing with n?

If Syntax:
Code:
if [ condition ]; then
    command
else 
    command
fi
(You can also do elsif)

If you want to get the return from a shell command you need to enclose the command in `command` or $(command) eg:
Code:
if [ $(ls -al | wc -l) -lt 5 ]; then 
  echo "less than 5 files"; 
fi
For Syntax:
Code:
for (( setup; condition; increment )); do
    command
done
Foreach syntax:
Code:
for i in dataset; do
    command
done
While syntax:
Code:
while [ condition ]; do
    command
done
Putting 'do' on next line is perfectly valid also (for, while, and until), as is putting 'then' on the following line (if), you can skip the ; by doing that but pick one and keep it consistent for readability's sake.

Head -n:
Code:
head -n# filename[s]
$@:
Code:
#!/bin/bash
echo "$0 is the name of the script"
echo "$1 is the first argument"
echo "$2 is the second argument"
echo "$3 is the third argument"
Sed one-liner to get a specific line:
Code:
sed -n '8p' filename
(Pulls the 8th line)

Awk version:
Code:
awk 'NR==8' filename

Last edited by rweaver; 01-05-2010 at 03:46 PM.
 
1 members found this post helpful.
Old 01-05-2010, 03:27 PM   #3
dr_0909
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
#!/bin/bash

echo linenum
read n

echo enter files
read filea fileb

for(n;$n -ge null;n+=1)];
do
if [wc -l <$filea -ge $fileb ||diff $filea $fileb >/dev/null ]
then

echo "same"
else
echo "different"
done

doesnt worked but trying still.how about this?I have chosen for loop now. I thought while can be more suitable maybe.
n is for the number of the line;doesnt it necessary(because of the head)??
but I think it can be done by head -n but I cant use head -n filea fileb in the text(when I write on the terminal it works) why?
can If statements be combined??
(p.s:and thanks very much for your help)

Last edited by dr_0909; 01-05-2010 at 03:34 PM. Reason: sorry I couldnt see the last codes above,I am new at the forum
 
Old 01-06-2010, 02:02 PM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Code:
if [wc -l <$filea -ge $fileb ||diff $filea $fileb >/dev/null ]
is not executing because you're not enclosing it.

Code:
if [$(wc -l < $filea) -ge $fileb || $(diff $filea $fileb >/dev/null) ]
and... that isn't going to do what you think it is.

This is what your code says:

If the word count of lines of filename-of-filea is greater than or equal to filename-of-fileb or if diff returns a true value for filea/b then fall into the loop.

Code:
if [ 9 -ge filename || False ]
So lets say we had a document 10 lines long called FILE_A and a document 20 lines long called FILE_B then... you'd get:

Code:
if [ 10 -ge FILE_B || False ]
Which both sides will fail every time.

You might want to experiment with a test program using diff also because the way you're expecting it to behave isn't how it behaves... like such:
Code:
#!/bin/bash
# Setup basic testing enviroment
echo "Filea" > filea
echo "Fileb" > fileb
cp filea filec

# Compare file a to file c
echo "diff filea filec (same)"
if [ $(diff filea filec > /dev/null) ]; then
  echo "1:True"
else
  echo "1:Not True"
fi

# Compare file a to file b
echo "diff filea fileb (different)"
if [ $(diff filea fileb > /dev/null) ]; then
  echo "2:True"
else
  echo "2:Not True"
fi

rm filea fileb filec
Make sure you put code in your messages in [ code ] ... [ /code ] tags (minus the spaces) in your reply, makes it a heck of a lot easier to read as it maintains formatting...

Last edited by rweaver; 01-06-2010 at 02:30 PM.
 
1 members found this post helpful.
Old 01-07-2010, 01:31 PM   #5
dr_0909
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Code:
#!/bin/bash

echo " $n $filea $fileb"
read n filea fileb

diff $filea $fileb > /dev/null
head -$n $filea $fileb > /dev/null

if [ `wc -l < $fileb` -ge $n  || `wc -l < $filea` -ge $n  ];

then

echo "different"

else 

echo "same"

fi
Aint I getitng close to execute??
It only gives error about if(and all the outputs are "same")
thanks for the codes I tried and changed where they give error,I think I understand the idea
 
Old 01-10-2010, 05:00 AM   #6
dr_0909
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
I think I am stuck here. when I wrote the code , ubuntu terminal doesnt answer anymore.
 
Old 01-10-2010, 03:09 PM   #7
kofucii
Member
 
Registered: May 2007
Location: Bulgaria
Distribution: Slackware, SCO Unix
Posts: 62

Rep: Reputation: 20
Try this:
Code:
#!/bin/bash

NUM=$1          # line number
FILEA=$2        # file 1
FILEB=$3        # file 2
#
# Help
#
USAGE () {
printf "USAGE: $0 <line number> <file1> <file2> \n"
exit 0
}
#
# Compare Lines
#
DO () {
LINE1=`sed -n ${NUM}p $FILEA`  # extract line from file 1
LINE2=`sed -n ${NUM}p $FILEB`  # extract line from file 2


if [ "$LINE1" == "$LINE2" ];then
        echo same
else
        echo different
fi

}
#
# Main
#
case $NUM in

        -h|--h|-help|--help)
                USAGE
        ;;
        *)
                DO
        ;;
esac

exit 0
 
1 members found this post helpful.
Old 01-11-2010, 02:23 PM   #8
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
You can give a man code and fix his program for a day, you can teach a man to code and he can fix his programs for a lifetime.

/bonk he was doing really well with learning the basics.

To the original poster, keep at it and you can get it to work, you were doing a good job.
 
1 members found this post helpful.
Old 01-12-2010, 02:25 PM   #9
dr_0909
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks both of you(Thanks for the code but I couldnt execute it)
Im sorry being late but Ive been busy with installing ubuntu again but it was not as easy as the first installation.(I am really new to this whole thing).

Code:
#!/bin/bash

echo " $n $filea $fileb"
read n filea fileb

diff $filea $fileb > /dev/null
head -$n $filea > /dev/null
head -$n $fileb > /dev/null

if [ $`sed -n ${NUM}p $filea` == $`sed -n ${NUM}p $fileb` ];

then

echo "different"

else

echo "same"

fi
I just wanted a simplier code than you've sent (like this maybe but I think this one is not right either)..
It seems doing what I want it to do(first print the variables, then reads it; then finds the differences and the first lines and here comes the loop if n of filea == n of fileb print same thats all)

should I necessary to use "USAGE exit"??
and do we have to send all the variables to another file or somewhere to make the loop do the operations?? i.e C program doesnt need it
for example > /dev/null
I am working on sth like and doesnt fit anywhere too;
Code:
if [ {`diff $`head -$n $filea`} || `diff $`head -$n $fileb | wc -l`} -eq 0 ]
(p.s:Although I havent fully solved this , my new assignments have started to come,and they are not seemed as short and simple as the ones before and I am not getting used to this scripting thing but thanks for your thoughts )
(p.ss:Im `she` by the way)
 
Old 01-12-2010, 03:58 PM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,038

Rep: Reputation: 97
Quote:
Originally Posted by dr_0909 View Post
Code:
if [ $`sed -n ${NUM}p $filea` == $`sed -n ${NUM}p $fileb` ];

then
$(command) or `command` are valid, but $`command` isn't.

Code:
if [ $(sed -n ${NUM}p $filea) == $(sed -n ${NUM}p $fileb) ];then
 
1 members found this post helpful.
Old 01-13-2010, 04:13 PM   #11
dr_0909
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
thanks it fixed the "too many arguments" error

Code:
#!/bin/bash

echo " n filea fileb"
read n filea fileb

diff $filea $fileb > /dev/null
head -$n $filea $fileb > /dev/null

if [ `wc -l < $filea` -ge $n ] && [ `wc -l < $fileb` -ge $n ];

then

echo "same"

else 

echo "different"

fi
isnt it great????????I couldnt hand it on time but I have learned much with editing)
thankssss a lot
Now I have to start to the other ones
 
Old 01-14-2010, 01:36 PM   #12
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Congratulations you're on the road to learning bash. It's a great shell and good scripting language. Post up when you need help and someone will be glad to help you.
 
Old 01-14-2010, 05:45 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
You may find these links useful if you haven't already got them:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


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
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 03:40 PM
Bash script: return code ftp TalkingMarble Linux - Newbie 3 05-09-2008 10:37 AM
Possible to have perl code into bash script ? frenchn00b Programming 3 07-03-2007 01:56 PM
couldnt able to run games kalabharath Linux - Software 7 03-27-2007 09:51 AM
Unusual piece of bash script code raypen Slackware 2 03-18-2006 11:49 PM


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