Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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"
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
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...
#!/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
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;
(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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.