ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
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.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
Rep:
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
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
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
Rep:
Quote:
Originally Posted by ta0kira
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
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.
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
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.
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.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
Rep:
Quote:
Originally Posted by catkin
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.