I'm building a script that uses a while loop like the following:
$sync=0
while [ $sync -eq 0 ];
do
body of the loop where $sync get changed sevaral times
done
The probleme is when I execute the script it gives me an error saying:
enter code hereline 53: 0=0: command not found
Please help me, thanks in advance
Change $sync=0 to sync=0. bash is not perl, so don't use the $ when assigning to a variable.
See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameters
PS, the error message is useful: what is on line 53 in your script?
Related
Any help is appreciated:
I am running a simple java currency converter application and trying to test it using conditional bash statements. For this test I am seeking to test when the user doesn't enter any value into the application what the resulting output is. Here is the bash script:
#!/bin/bash
input=""
expectedOuput="Please enter correct input"
actualOutput=$(java CurrencyConverter $input)
if [ $expectedOutput == $actualOutput ]; then
echo "Test 1 Passed"
else
echo "Test 1 Failed"
fi
I am aiming for the test to fail and echo this, however the script just returns the java error instead of echoing, like this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at CurrencyConverter.main(CurrencyConverter.java:12)
I am newer to bash scripting so have been changing the syntax, I just cant seem to stop the java error being printed
The exception message is printed to stderr, so if you want to get it in actualOutput (perhaps misleading name now):
actualOutput=$(java CurrencyConverter "$input" 2>&1)
you can also check the exit status of the java command.
Also check misspelled expectedOuput and use of quotes.
Use https://www.shellcheck.net/.
And the ArrayIndexOutOfBoundsException it's definitely something you should fix in your java code.
I'm trying to learn how to write some basic functions in Ubuntu, and I've found that some of them work, and some do not, and I can't figure out why.
Specifically, the following function addseq2.sh will work when I source it, but when I just try to run it with bash addseq2.shit doesn't work. When I check with $? I get a 0: command not found. Does anyone have an idea why this might be the case? Thanks for any suggestions!
Here's the code for addseq2.sh:
#!/usr/bin/env bash
# File: addseq2.sh
function addseq2 {
local sum=0
for element in $#
do
let sum=sum+$element
done
echo $sum
}
Thanks everyone for all the useful advice and help!
To expand on my original question, I have two simple functions already written. The first one, hello.sh looks like this:
#!/usr/bin/env bash
# File: hello.sh
function hello {
echo "Hello"
}
hello
hello
hello
When I call this function, without having done anything else, I would type:
$ bash hello.sh
Which seems to work fine. After I source it with $ source hello.sh, I'm then able to just type hello and it also runs as expected.
So what has been driving me crazy is the first function I mentioned here, addseq2.sh. If I try to repeat the same steps, calling it just with $ bash addseq2.sh 1 2 3. I don't see any result. I can see after checking as you suggested with $ echo $?that I get a 0 and it executed correctly, but nothing prints to the screen.
After I source it with $ source addseq2.sh, then I call it just by typing $ addseq2 1 2 3 it returns 6 as expected.
I don't understand why the two functions are behaving differently.
When you do bash foo.sh, it spawns a new instance of bash, which then reads and executes every command in foo.sh.
In the case of hello.sh, the commands are:
function hello {
echo "Hello"
}
This command has no visible effects, but it defines a function named hello.
hello
hello
hello
These commands call the hello function three times, each printing Hello to stdout.
Upon reaching the end of the script, bash exits with a status of 0. The hello function is gone (it was only defined within the bash process that just stopped running).
In the case of addseq2.sh, the commands are:
function addseq2 {
local sum=0
for element in $#
do
let sum=sum+$element
done
echo $sum
}
This command has no visible effects, but it defines a function named addseq2.
Upon reaching the end of the script, bash exits with a status of 0. The addseq2 function is gone (it was only defined within the bash process that just stopped running).
That's why bash addseq2.sh does nothing: It simply defines (and immediately forgets) a function without ever calling it.
The source command is different. It tells the currently running shell to execute commands from a file as if you had typed them on the command line. The commands themselves still execute as before, but now the functions persist because the bash process they were defined in is still alive.
If you want bash addseq2.sh 1 2 3 to automatically call the addseq2 function and pass it the list of command line arguments, you have to say so explicitly: Add
addseq2 "$#"
at the end of addseq2.sh.
When I check with $? I get a 0: command not found
This is because of the way you are checking it, for example:
(the leading $ is the convention for showing the command-line prompt)
$ $?
-bash: 0: command not found
Instead you could do this:
$ echo $?
0
By convention 0 indicated success. A better way to test in a script is something like this:
if addseq.sh
then
echo 'script worked'
else
# Redirect error message to stderr
echo 'script failed' >&2
fi
Now, why might your script not "work" even though it returned 0? You have a function but you are not calling it. With your code I appended a call:
#!/usr/bin/env bash
# File: addseq2.sh
function addseq2 {
local sum=0
for element in $#
do
let sum=sum+$element
done
echo $sum
}
addseq2 1 2 3 4 # <<<<<<<
and I got:
10
By the way, an alternative way of saying:
let sum=sum+$element
is:
sum=$((sum + element))
I have the following logic in the script Setup.sh.
#!/bin/bash
for ((i = 0 ; i < 5 ; i++))
do
echo "Welcome $i times."
done
When I run the script using the command ./Setup.sh, I get the error
./Setup.sh: line 3: syntax error near unexpected token `(('
./Setup.sh: line 3: `for ((i = 0 ; i < 5 ; i++))'
When I run the script using the command sh Setup.sh , I get the error
Setup.sh: syntax error at line 3: `(' unexpected
When I run the script logic in Execute BASH Shell Script Online using http://www.compileonline.com/execute_bash_online.php, it executes perfectly and prints the following.
Welcome 0 times.
Welcome 1 times.
Welcome 2 times.
Welcome 3 times.
Welcome 4 times.
Can someone help me understand why I get this error on Sun Solaris Unix machine?
When you run sh Setup.sh the Solaris /bin/sh is used to execute the script. The Solaris /bin/sh is not a POSIX shell and also does not understand the non-portable (()) syntax.
If you use #!/bin/bash it should work. If it doesn't, maybe your bash is very ancient. What does bash --version output?
The online demo uses bash 4.1.2(1)-release.
Please check which version of bash you have on the Solaris system.
bash --version
As far as I remember, the (( )) arithmetic notation was introduced recently. And it's a bashism, so it does not work with sh.
The website probably uses a new version of bash.
I would like to pass parameters to a perl script using positional parameters inside a bash script "tablecheck.sh". I am using an alias "tablecheck" to call "tablecheck.sh".
#!/bin/bash
/scripts/tables.pl /var/lib/mysql/$1/ /var/mysql/$1/mysql.sock > /tmp/chktables_$1 2>&1 &
Perl script by itself works fine. But when I do "tablecheck MySQLinstance", $1 stays $1. It won't get replaced by the instance. So I get the output as follows:
Exit /scripts/tables.pl /var/lib/mysql/$1/ /var/mysql/$1/mysql.sock > /tmp/chktables_$1 2>&1 &
The job exits.
FYI: alias tablecheck='. pathtobashscript/tablecheck.sh'
I have a bunch of aliases in another bash script. Hence . command.
Could anyone help me... I have gone till the 3rd page of Google to find an answer. Tried so many things with no luck.
I am a noob. But may be it has something to do with it being a background job or $1 in a path... I don't understand why the $1 won't get replaced...
If I copy your exact set up (which I agree with other commenters, is some what unusual) then I believe I am getting the same error message
$ tablecheck foo
[1]+ Exit 127 /scripts/tables.pl /var/lib/mysql/$1/ /var/mysql/$1/mysql.sock > /tmp/chktables_$1 2>&1
In the /tmp/chktables_foo file that it makes there is an additional error message, in my case "bash: /scripts/tables.pl: No such file or directory"
I suspect permissions are wrong in your case
I'm start to playing around with ShellScript Unix, so maybe it's a silly question. Apologies if that's the case.
I was trying to handle the exit codes to properly address adverse situations in my code, and for this, I created a code snippet to understand the unix exit behavior. Here it is:
#!/usr/bin/bash
RES=1
if [ $RES -eq 0 ]
then
echo "Finishing with success!"
exit 0
else
echo "Finishing with error!"
exit 1
fi
I understood that, once the code is called (and terminated) I'd go back to bash prompt. However, it seems the exit instruction is also leaving bash. Is it normal? Maybe it's something related to my development environment?
Here are the messages...
bash-3.00$ . errtest.sh
Finishing with error!
$ echo $?
1
$ bash
bash-3.00$ which bash
/usr/bin/bash
For reference, I've added the return and the bash location. Hope it helps.
Thanks in advance!
This is because you're sourcing the script in your current environment (by using the . command). Try executing the script with either:
bash ./errtest.sh
or by giving the necessary permissions to the script file and executing it like this:
chmod u+x ./errtest.sh
./errtest.sh