LinuxQuestions.org
Review your favorite Linux distribution.
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 12-24-2010, 04:20 AM   #1
wang
Member
 
Registered: Dec 2010
Distribution: ubuntu 10.04
Posts: 43

Rep: Reputation: 0
Smile unable to execute simple bash scripts


hi,friends! All of a sudden,i can't execute a simple script as follows:
######################################################################
#!/bin/bash
var1=5
var2=4
if (( var1 > var ))
then echo "$var1 is greater than $var2"
fi

exit 0
#######################################################################
result:./444: 6: var1: not found (/home/wang/444)
and:
######################################################
#!/bin/bash
a=5
b=4
if (( a > b))
then echo "good"
else echo "bad"
fi
if (( "$a" > "$b" ))
then echo "success"
else echo "failure"
fi
exit
#####################################################
result:sh: Can't open ./new (/home/wang/new)

i am quite confused but i really can't find out why.is there anything wrong ?
thanks for your help!

Last edited by wang; 12-24-2010 at 04:22 AM.
 
Old 12-24-2010, 04:28 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
Well they're just invalid scripts, you're code is very wrong. For example, you are tring to call a variable, var1, without presuming it with a $ sign. Even within a few lines of code you've done this right and then wrong.
 
Old 12-24-2010, 04:47 AM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
These characters at the begining:
Quote:
######################################################
are part of the code? It shouldn't. And for code use [CODE] blocks, for better readability. In the second example you have not give an execute permission, the first work me as expected. Check that you have installed bash and /bin/bash is not a symlink to other shell.

Last edited by eSelix; 12-24-2010 at 04:50 AM.
 
Old 12-24-2010, 04:54 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

I assume that
if (( var1 > var ))

is really a typo and that it should be
if (( var1 > var2 ))

Anyway, I executed both your scripts and they do work as expected. So my guess would also be a permission issue. Can post the permissions of the files as well as the perms of the containing directories?
 
Old 12-24-2010, 04:55 AM   #5
kaushalpatel1982
Member
 
Registered: Aug 2007
Location: INDIA
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, PCLinuxOS
Posts: 153

Rep: Reputation: 9
Correct Example 1
Code:
#!/bin/bash
var1=5
var2=4
if (( $var1 > $var2 ))
then echo "$var1 is greater than $var2"
fi

exit 0
==============================================
Correct Example 2

Code:
#!/bin/bash
a=5
b=4
if (( $a > $b ))
then echo "good"
else echo "bad"
fi
if (( "$a" > "$b" ))
then echo "success"
else echo "failure"
fi
exit
 
Old 12-24-2010, 05:04 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by kaushalpatel1982 View Post
Correct Example ...
Except for the typo in the OP the scripts are indeed correct. You do not need to prepend a '$' in this case to perform the comparison. When you use the '((' operator then
if (( var1 > var2 ))

is valid syntax. At least it is in my bash (v4.1.5).
 
Old 12-24-2010, 07:05 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
crts beat me to it but he is 100% correct that $ does not need to be used in the (()) mechanism.
Also, whilst it may work, I would class the following as incorrect:
Code:
if (( "$a" > "$b" ))
Reason being is that you are comparing strings here but using (()) which is designed for numeric testing.
 
Old 12-24-2010, 09:39 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
first line of a script

Quote:
Originally Posted by eSelix View Post
These characters at the begining:
are part of the code? It shouldn't. And for code use [CODE] blocks, for better readability. In the second example you have not give an execute permission, the first work me as expected. Check that you have installed bash and /bin/bash is not a symlink to other shell.
Hi eSelix,

since I always work with bash I did not consider that other shells might be picky about the first line. Well observed!

@OP: The first line of a script should always be the shebang. If I invoke the following
Code:
###########################
#!/bin/bash
a=5
b=4
if (( a < b))
then echo "good"
else echo "bad"
fi
exit
from within csh then I get the following error:
Code:
$ /bin/csh -c './script' 
a=5: Command not found.
b=4: Command not found.
if: Expression Syntax.
Since the first line is not a shebang the script gets executed in csh instead of bash. Even an empty first line results in an error message. Bash does not have a problem with that.

You are probably not calling the script from a bash shell. In this case follow eSelix advice and make sure that your first line is
#!/bin/bash

This should "fix" your scripts.

Last edited by crts; 12-24-2010 at 09:41 AM.
 
Old 12-25-2010, 09:26 AM   #9
wang
Member
 
Registered: Dec 2010
Distribution: ubuntu 10.04
Posts: 43

Original Poster
Rep: Reputation: 0
thanks for all your replies.now i think the scripts are right but ...

the four lines "#############################################################"don not belong to the scripts.
now the question is that i can't just execute the simplest script,but i can execute the scripts before!
how interesting!
and i am sure that my shell is bash by default(i had never changed it !)
by using "printenv", i find the line "SHELL=/bin/bash" ,is that right?
or who can tell me what to do ?maybe set the envirenment varible or something else ?
thanks for your kindness!
 
Old 12-25-2010, 10:55 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Please copy and paste from a command prompt session, showing the commands you used to run the scripts and the resulting output -- in CODE tags (use the # button in Advanced posting mode).
 
Old 12-25-2010, 10:10 PM   #11
wang
Member
 
Registered: Dec 2010
Distribution: ubuntu 10.04
Posts: 43

Original Poster
Rep: Reputation: 0
Unhappy still 2 problems...

example 1:
#!/bin/bash

echo

let "a=11"
let "a=a+5"

echo "11 + 5 = $a"

let "a <<= 3"
echo "\"\$a\" (=16) left-shifted 3 places = $a"


result:
wang@wang-laptop:~$ sh ./5

./5: 5: let: not found
./5: 6: let: not found
11 + 5 =
./5: 10: let: not found
"$a" (=16) left-shifted 3 places =
./5: 14: let: not found
128 / 4 =
./5: 17: let: not found
32 - 5 =
./5: 20: let: not found
27 * 10 =
------------------------------------------------------------------------

example 2:
#!/bin/bash
var1=5
var2=4
if (( var1 > var2 ))
then echo "$var1 is greater than $var1"
fi


result:
wang@wang-laptop:~$ sh ./3
./3: 6: var1: not found

-----------------------------------------------------------------------
i really want to know why the results happen:
"./5: 5: let: not found"(example 1) and "./3: 6: var1: not found"(example 2)

wang@wang-laptop:~$ echo $BASH_VERSION
4.1.5(1)-release

so annoying but i cannot get rid of it! thanks again!!!
 
Old 12-25-2010, 11:05 PM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Ok,

please start using CODE tags! Catkin gave you failproof instructions on how to accomplish that. See also my signature.

As for your problem, answer the following questions first:
- Are those really the exact errors that get printed? They should look more like
Code:
./5: line 5: let: command not found
- Are you 100% sure that the scripts are called from within a bash-shell?
- Is your very first line indeed the shebang ( and not, e.g. emtpy)?
- Are you using some sort of 'funny' editor that does precede your lines with line-numbers?
- What does your PATH environment variable look like?
 
Old 12-26-2010, 12:37 AM   #13
wang
Member
 
Registered: Dec 2010
Distribution: ubuntu 10.04
Posts: 43

Original Poster
Rep: Reputation: 0
well,it looks so weird.
in fact,the shell does as expected when running a single command ("let ..."),but has problems in scripts given above.
i wonder if this is caused by virus or something mistaken.
can anything else cause problems like this?
your kindness!
 
Old 12-26-2010, 01:52 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by wang View Post
well,it looks so weird.
in fact,the shell does as expected when running a single command ("let ..."),but has problems in scripts given above.
i wonder if this is caused by virus or something mistaken.
can anything else cause problems like this?
your kindness!
We may never know because you have not answered our questions, questions designed to get the information we need to analyse this problem.
 
Old 12-26-2010, 01:55 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
wang - try and answer the questions being asked of you and then maybe we can start to help.
 
  


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
[SOLVED] Why does perl execute bash scripts? zensunni Linux - Newbie 3 11-13-2010 04:24 PM
Another simple question - cannot execute scripts Stevie-B-242 Linux - Newbie 3 02-18-2008 05:53 AM
how to execute bash scripts from C????? :( cho Programming 2 06-19-2005 08:15 PM
Unable to execute sh scripts zowey Mandriva 2 12-23-2004 04:59 PM
execute bash scripts vapor Programming 9 07-14-2003 03:54 PM


All times are GMT -5. The time now is 04:07 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