I am trying to run my shell script from command line lets say;
my script looks like this:
#!bin/bash
echo hello
When try to run this source ./abcd.sh I get this error.
"' is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found
: command not found
hello
"
Never seen this before something wrong with having a empty line before "echo hello" ? I was wondering if anyone else encountered something like this.
Along with the first line of your script being a comment, it sounds like your file has DOS line endings, and the carriage return is being treated as the command that isn't found. The error message sounds like something provided by a custom command_not_found_handle function (which I believe Ubuntu defines).
#!bin/bash
needs to be
#!/bin/bash
or wherever bash is installed (you can locate this by doing whereis bash).
Your program should work fine when invoked using bash, i.e., bash ./abcd.sh, but when executed directly ./abcd.sh then the hashbang line does matter because that is how the interpreter is located for the script contained in the executable file.
Try echo 'hello', within quotes. It looks like there is a newline between the echo command and hello and it is trying to run 'hello' as a command.
The hashbang line should be #!/bin/bash, but messing that up won't matter as it will interpret any line that starts with a hash as a comment.
Run script with debug option to see which line actually is failing:
bash -x abcd.sh
Note: in this case the Shebang line will be treated as a comment, so if the rest of your script is correct, it will execute correctly:
$ bash -x abcd.sh
+ echo hello
hello
Make sure your file does not have a BOM
I had the same problem when editing a script under Windows with Notepad++.
make sure to convert to "UTF-8 witout BOM".
Related
I am getting confused about Linux shells. It may be that I oversee something obvious as a Linux noob.
All I want is the following script to run:
#!/bin/bash
echo "Type some Text:"
read var
echo "You entered: "$var
Here is the situation:
Installed Ubuntu Server 14.04 in a VirtualBox on windows
Installed it with this packages
A SAMBA mounted on /media/buff
The script is on /media/buff/ShellScript/test.sh
made executable by "sudo chmod a+x /media/buff/ShellScript/test.sh"
The rest is default
I am using PSPad on windows to edit the script file
I read about the dash but I'm not getting it.
Here are some variations:
Using sh to launch
user#Ubuntu:/media/buff/ShellScript$ sh test.sh
Type some Text:
:bad variable nameread var
You entered:
Using bash to launch:
user#Ubuntu:/media/buff/ShellScript$ bash test.sh
Type some Text:
': Ist kein gültiger Bezeichner.var (means no valid identifyier)
You entered:
Changed the Shebang in the script to "#!/bin/sh", Using sh to launch
user#Ubuntu:/media/buff/ShellScript$ sh test.sh
Type some Text:
:bad variable nameread var
You entered:
I searched across the Internet for hours now and I assume, that the script itself is ok, but there are missing some environment settings.
I used "sudo dpkg-reconfigure dash" to set dash as default shell (which I think is ubuntu default anyway)
sadface panda :)
There are most probably carriage returns (CR, '\r') at the end of your lines in the shell script, so the variable is trying to be read into var\r instead of var. This is why your error message is meaningless. A similar error should look like this:
run.sh: 3: export: [... the variable name]: bad variable name
In your case bash throws an error because var\r is illegal for a variable name due to the carriage return, so it prints
test.sh: 3: read: var\r: bad variable name
but the \r jumps to the beginning of the line, overwriting the start of the error message.
Remove the carriage returns fom the ends of lines, possibly by using the dos2unix utility.
Here are a list of editors that support unix newline character.
Brackets has an extension for new-line/end-of-line support and it is built-in in Notepad++. Go to the 'Edit' tab. Find 'EOL Conversions' and select Unix (LF).
That should get it done.
A script like the following:
#!/bin/bash
function hello {
echo "Hello World"
}
hello
will work fine but when I call it with nohup
nohup ./myScript.sh
the script no longer works and the following is printed:
./myScript.sh: 5: ./myScript.sh: function: not found
Hello World
./myScript.sh: 9: ./myScript.sh: Syntax error: "}" unexpected
I know that there is no point in using nohup for this script since it runs fairly quick but I have another script that is going to take several hours to run and I have used that function syntax in it. Is there a way to fix this?
Note that to declare a function in POSIX standard shell, you just write the function name followed by a pair of empty parentheses, and a body in curly brackets:
#!/bin/bash
hello() {
echo "Hello World"
}
hello
Some shells, like bash, also accept the function keyword to declare functions, but based on your error message, it looks like the shell that you are using does not accept it. For portability, it would be best to use the POSIX standard declaration style.
The reason you see the difference between running the script directly and running the script under nohup is the blank lines at the beginning of the file. The #! need to be the first two characters of the file in order to specify the interpreter, otherwise that is just a comment. It looks like when executed from within a Bash shell, the script gets interpreted by Bash and it recognizes the Bash extension. But when run from nohup, it gets interpreted by sh, which on some systems is a more bare-bones shell like dash that only supports the POSIX syntax above.
I'm trying to execute a very simple script with cygwin, composed of:
#!/bin/bash\n
echo "hi"\n
with cygwinpath\bin\bash.exe /cygdrive/c/my_path/test.bash
but it says
/cygdrive/c/my_path/test.bash: line 1: #!/bin/bash: No such file or directory
However, it still prints 'hi'.
Why is this, and how to fix it ?
Thanks.
The first line of your script should just be #!/bin/bash and not #!/bin/bash\n
The code is still executing because the heading #!/bin/bash specifies a shell, and echo "hi"\n is a command to the terminal.
As for your issue I'm having no problems running it using the following path in the cygwin terminal:
/cygdrive/c/<my_path>/bin/bash.exe /home/user/test.bash
If I run this script:
#!/bin/bash
PROJECT_PATH="/Users/hudson/workspace/Foo"
XCODE_PROJECT_FOLDER="${PROJECT_PATH}/CODE/APP/FOO_IOS"
echo ${PROJECT_PATH}
echo ${XCODE_PROJECT_FOLDER}
It displays:
/Users/hudson/workspace/Foo
/Users/hudson/workspace/Foo/CODE/APP/FOO_IOS
If I put the variables in another file, include it in the main script file, and run it:
test.sh
#!/bin/bash
. "/Users/hudson/workspace/Foo/ota.sh"
echo ${PROJECT_PATH}
echo ${XCODE_PROJECT_FOLDER}
/Users/hudson/workspace/Foo/ota.sh
#!/bin/bash
PROJECT_PATH="/Users/hudson/workspace/Foo"
XCODE_PROJECT_FOLDER="${PROJECT_PATH}/CODE/APP/FOO_IOS"
I have this output:
: command not found /Users/hudson/workspace/Foo/ota.sh: line 2:
/Users/hudson/workspace/Foo
/CODE/APP/FOO_IOSkspace/Foo
Any idea of where the problem could come from?
If I put ota.sh in the same folder as test.sh, this works well
If I don't let a blank line between #!/bin/bash and the inclusion, I don't get the : command not foundpace/Foo/ota.sh: line 2 message
Probably wrong/mixed unix/windows line endings, try to fix it with dos2unix.
Try opening the file in vim, to see if there are any special characters there, like backspace.
As with the previous answer, probably wrong/mixed unix/windows line endings. If you are using notepad++,as Mike mentioned, in notepadd++, you can change the EOL character(s) by choosing the Edit menu, then EOL Conversion. After reading Mike's comment, that is what fixed this same exact problem that the op mentioned, that I was having too.
I'm having trouble with a shell script in Cygwin. The specific command that's causing the problem is this:
sed -e "s/6.0.[0123456789]\{1,\}/6.0.${REV}/g" "path/to/file/config.xml" > "path/to/file/config.xml.tmp"
The problem is that the file is being created with a strange character at the end, so instead of being named config.xml.tmp, it's named "config.xml.tmp". From the "ls" command and from the windows command prompt, it looks like "config.xml.tmp?"
If I run the sed command just from the shell, the file named correctly, and the script works fine in Linux.
Any idea what could be wrong? Thanks!
My guess is that your script file doesn't have UNIX line endings. The \r character in the windows line ending is what's getting added to the end of your filename. You can check with od or hexdump to see if that's the problem.