LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2009, 07:54 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Bash script has errors


First, i am a newbie with bashscripts.
I wrote a bash script that is suppose to check if two files have equal checksome; but it has errors.
here are the errors :
Code:
./chk.sh: line 2: [pw: command not found
./chk.sh: line 2: ./pw: Permission denied
./chk.sh: line 9: [Files - and .cksm are identical: command not found
Files do not match
./chk.sh: line 17: end: command not found
and the script:
Code:
#/bin/bash
if ["$1" = "" || "$2" = ""]:
then
echo NO ARGS
killall $0
fi
md5sum $1 > .cksm
md5sum $2 | diff -s - .cksm > .cksm2
if ["$(more .cksm2)" = "Files $1 and $2 are identical"]:
then
echo Files match
else 
echo Files do not match
fi
rm .cksm
rm .cksm2
end

Last edited by smeezekitty; 11-17-2009 at 08:03 PM.
 
Old 11-17-2009, 08:26 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
You could try this:
Code:
#/bin/bash
if [[ "$1" = "" || "$2" = "" ]]
then
        echo NO ARGS
        exit 1
fi
diff -s <(md5sum $1 | awk '{print $1}') <(md5sum $2 | awk '{print $1}')
The output is a little messy, it talks about file descriptors instead of the file names.

I'd suggest reading the Advanced Bash Scripting Guide, it's got a range of techniques and good explanations.

Last edited by gilead; 11-17-2009 at 08:31 PM.
 
Old 11-17-2009, 08:31 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can just use cmp
Code:
$ cmp file1 file2
$ echo $? #0 means equal
 
Old 11-17-2009, 09:37 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by ghostdog74 View Post
you can just use cmp
Code:
$ cmp file1 file2
$ echo $? #0 means equal
doesnt actually compare the checksome.
 
Old 11-17-2009, 09:52 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
This works, any suggestions on how to improve it? code looks messy.
Code:
#!/bin/bash
if [[ "$1" = "" || "$2" = "" ]]
then
echo NO ARGS
exit
fi
md5sum $1 > .cksm
md5sum $2 | diff - .cksm > .cksm2
if ["$(more .cksm2)" = ""];
then
clear;echo Files match
else 
clear;echo Files do not match
fi
rm .cksm
rm .cksm2
end
 
Old 11-17-2009, 09:57 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by smeezekitty View Post
doesnt actually compare the checksome.
comparing checksum is the same as comparing file contents. if you really want to compare checksum,
Code:
$ md5sum file1 file2 | uniq -d -w32 |wc -l
if the result is 1, then they are the same.
 
Old 11-17-2009, 10:01 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Your problem is that you don't have spaces after [ and before ]; that means the shell concatenates what's next to it, preventing it from finding an appropriate command.
Kevin Barry

PS Why do you end lines with :?

Last edited by ta0kira; 11-17-2009 at 10:04 PM.
 
Old 11-17-2009, 10:12 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by ghostdog74 View Post
comparing checksum is the same as comparing file contents. if you really want to compare checksum,
Code:
$ md5sum file1 file2 | uniq -d -w32 |wc -l
if the result is 1, then they are the same.
wow that works good, i did not even know about the uniq command.
 
Old 11-17-2009, 10:14 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by ta0kira View Post
Your problem is that you don't have spaces after [ and before ]; that means the shell concatenates what's next to it, preventing it from finding an appropriate command.
Kevin Barry
I am used to C where spaces dont matter much
Quote:

PS Why do you end lines with :?
A site online showed it with a :
 
Old 11-17-2009, 11:16 PM   #10
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by smeezekitty View Post
I am used to C where spaces dont matter much
A site online showed it with a :
Interesting. ':' is equivalent to 'true' in the shell (both are generally shell builtins). I'm interested what the site that you picked that up from had in mind.

Last edited by bartonski; 11-17-2009 at 11:17 PM.
 
Old 11-18-2009, 12:15 AM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by bartonski View Post
Interesting. ':' is equivalent to 'true' in the shell (both are generally shell builtins). I'm interested what the site that you picked that up from had in mind.
That's only if it's the command, though. Could be a typo for ;.
Kevin Barry
 
Old 11-18-2009, 10:32 AM   #12
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by ta0kira View Post
That's only if it's the command, though. Could be a typo for ;.
Kevin Barry
true, it could be a typo, but you can also do something like this:

Code:
while :
do
    echo "I am in an infinite loop."
done
I'm interested to know if smeezekitty ran across a more sophisticated version of this.
 
Old 11-18-2009, 10:51 AM   #13
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 smeezekitty View Post
This works, any suggestions on how to improve it? code looks messy.
Code:
#!/bin/bash
if [[ "$1" = "" || "$2" = "" ]]
then
echo NO ARGS
exit
fi
md5sum $1 > .cksm
md5sum $2 | diff - .cksm > .cksm2
if ["$(more .cksm2)" = ""];
then
clear;echo Files match
else 
clear;echo Files do not match
fi
rm .cksm
rm .cksm2
end
As already posted there are neater ways of solving the problem but here are some suggestions about the shellscript itself in case they are of any interest. Mostly indentation so the logic/functional flow stands out. Also
  • changed "" to '' (heuristic: never use double quotes where single quotes will do)
  • escaped exit (with \exit) to defend against exit being an alias rather than the intended built-in
  • removed final "end" command (what did it do?)
Code:
#!/bin/bash
if [[ "$1" = '' || "$2" = '' ]]; then
    echo NO ARGS
    \exit
fi

md5sum $1 > .cksm
md5sum $2 | diff - .cksm > .cksm2

if [[ "$(more .cksm2)" = '' ]]; then
    clear; echo Files match
else 
    clear; echo Files do not match
fi

rm .cksm
rm .cksm2
You could add error traps to ensure that $1 and $2 are readable files.
 
Old 11-18-2009, 11:13 AM   #14
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Ending the script with a ':' is equivalent to having the last line of the script be:
exit 0
Having it at the end of each line will give each line of code an exit status of '0' which is probably not a good thing -I mean if you do error checking after running a line of code, having ':' at the end would destroy whatever exit code the line of code ahd generated.
 
Old 11-18-2009, 12:59 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by catkin View Post
As already posted there are neater ways of solving the problem but here are some suggestions about the shellscript itself in case they are of any interest. Mostly indentation so the logic/functional flow stands out. Also
  • changed "" to '' (heuristic: never use double quotes where single quotes will do)
  • escaped exit (with \exit) to defend against exit being an alias rather than the intended built-in
  • removed final "end" command (what did it do?)
Code:
#!/bin/bash
if [[ "$1" = '' || "$2" = '' ]]; then
    echo NO ARGS
    \exit
fi

md5sum $1 > .cksm
md5sum $2 | diff - .cksm > .cksm2

if [[ "$(more .cksm2)" = '' ]]; then
    clear; echo Files match
else 
    clear; echo Files do not match
fi

rm .cksm
rm .cksm2
You could add error traps to ensure that $1 and $2 are readable files.
Code:
./chk.sh chk.sh hello
<Clears screen>
Files do not match
bash: ./chk.sh: line 17: warning no 'end' statement at EOF
./chk.sh chk.sh chk.sh
<Clears screen>
Files match
bash: ./chk.sh: line 17: warning no 'end' statement at EOF
./chk.sh
NO ARGS

Last edited by smeezekitty; 11-18-2009 at 01:00 PM.
 
  


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
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Bash script can't handle errors when run by cron rosv Programming 4 01-08-2009 07:50 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Errors running bash script files bkdc Linux - Newbie 6 02-01-2008 07:44 AM
Bash script. Rsync and MSSQL errors and logging. nattflyger Programming 4 07-19-2006 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 PM.

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