bash in windows error? - bash

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

Related

Move linux command into shell script [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Bash script works manually but not as cron job [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Two commands in one script ubuntu terminal

I have two commands:
#!/bin/bash
python /srv/django/manage.py shell; execfile('/home/usr/myscript.py')
I want to use them in one script scr.sh. However as it is, I get errors.
CommandError: Command doesn't accept any arguments
/home/usr/project.sh: line 7: syntax error near unexpected token
`'/home/usr/myscript.py''
where myscript.py is what the code I want running when the python shell appears. How could I go about achieving this?
Thanks!
What was happening here is that ';' symbol will wait until something is performed/run in the shell. The correct syntax would be
#!/bin/bash
python /srv/django/manage.py shell <<EOF\ execfile('myscript.py') EOF

Shell scripting: new line command not found

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".

command errors in a file but runs fine on a shell

I am trying to run a command in a script, something like this one:
ssh user#host:/bin/echo > /home/path/file.log
Now when I run this command on a command line it works fine, however when put in a script (shell or ruby ) it cribs saying:
/bin/sh: /home/path/*.log: No such file or directory
Am I missing something?
Thanks!
Update:
It's weird that same thing is not being executed now even on the shell when I use putty. I have verified that the path and file exists on remote machine which is being ssh'ed into.
You need to loop over the files. If it works from the command line then your interactive shell is not a standard shell.
for f in /home/path/*.log; do
:>"$f"
done
Note also the use of a null command; in many shells, you don't need a command at all. Your echo puts an unattractive empty line at the beginning of each file.
If you are attempting to run this remotely, you will need to quote it:
ssh user#remote 'for f in /home/path/*.log; do :>"$f"; done'
Its working fine when I put quotes:
ssh user#host:"/bin/echo > /home/path/file.log"

Resources