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.
Related
I'm trying to write a simple script in tcsh (version 6.12.00 (Astron) 2002-07-23), but I am getting tripped up by the if-then-else syntax. I am very new to script writing.
This script works:
#!/bin/tcsh -f
if (1) echo "I disagree"
However, this one does not:
#!/bin/tcsh -f
if ( 1 ) then
echo "I disagree"
else
echo "I agree"
endif
For one thing, this code, when run, echoes both statements. It seems to me it should never see the else. For another, the output also intersperses those echoes with three iterations of ": Command not found."
Edited to add: here is the verbatim output:
: Command not found.
I disagree
: Command not found.
I agree
: Command not found.
I know that the standard advice is to use another shell instead, but I am not really in a position to do that (new job, new colleagues, everyone else uses tcsh, want my scripts to be portable).
When I copy-and-paste your script and run it on my system, it correctly prints I disagree.
When I change the line endings to Windows-style, I get:
: Command not found.
I disagree
: Command not found.
I agree
: Command not found.
So, your script very likely has Windows-style line endings. Fix the line endings, and it should work. The dos2unix command is one way to do that (man dos2unix first; unlike most UNIX text-processing commands, it replaces its input file.)
The problem is that tcsh doesn't recognize ^M ('\r') as an end-of-line character. It sees the then^M at the end of the line as a single command, and prints an error message then^M: Command not found. The ^M causes the cursor to return to the beginning of the line, and the rest of the message overwrite the then.
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
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".
I wrote a simple shell script to check for the existence of a xml file and if it exists, then rename an old xml file to be backup and then move the new xml file to where the old xml file was stored.
#!/bin/sh
oldFile="/Documents/sampleFolder/sampleFile.xml"
newFile="/Documents/sampleFile.xml"
backupFileName="/Documents/sampleFolder/sampleFile2.backup"
oldFileLocation="/Documents/sampleFolder"
if [ -f "$newFile" ] ; then
echo "File found"
#Rename old file
mv $oldFile $backupFileName
#move new file to old file's location
mv $newFile $oldFileLocation
else
echo "File not found, do nothing"
fi
However, every time I try to run the script, I get 4 command not found messages and a syntax error: unexpected end of file. Any suggestions on why I get these command not found errors or the unexpected end of file? I double checked that I closed all my double quotes, I have code highlight :)
EDIT:
output from running script:
: command not found:
: command not found:
: command not found1:
: command not found6:
replaceXML.sh: line 26: syntax error: unexpected end of file
I believe you're running on Cygwin. There's more to the error messages than what you're seeing:
: command not found:
: command not found:
: command not found1:
: command not found6:
replaceXML.sh: line 26: syntax error: unexpected end of file
You probably used a Windows editor to create the script file, which means it uses Windows-style CR-LF ("\r\n") line endings, rather than Unix-style LF ('\n') line endings. Some programs under Cygwin can handle either form, but the shell doesn't.
For example, the line that looks like
then
looks to the shell like
then^M
where ^M is the ASCII CR character. This would actually be a valid command name if it existed, but it doesn't, so the shell complains:
then^M: command not found
But printing the CR character causes the cursor to go back to the beginning of the line, so everthing before the : is overwritten.
You're getting the "unexpected end of file" message because the shell never saw a fi to match the if.
You can use the dos2unix command to fix the line endings. Be sure to read the man page (man dos2unix); unlike most text filters, dos2unix replaces its input file rather than writing to stdout.
I can't really see anything wrong with your code apart from then not being in a legal place for older shells. Also notice the quotes around arguments to mv (but that should not be a problem if the files are named properly).
Try this:
#!/bin/sh
oldFile="/Documents/sampleFolder/sampleFile.xml"
newFile="/Documents/sampleFile.xml"
backupFileName="/Documents/sampleFolder/sampleFile2.backup"
oldFileLocation="/Documents/sampleFolder"
if [ -f "$newFile" ]
then
echo "File found"
mv "$oldFile" "$backupFileName"
mv "$newFile" "$oldFileLocation"
else
echo "File not found, do nothing"
fi
PS: verify that /bin/sh is (or points to) a bourne based shell.
What I did in my case:
I used Bash On Ubuntu on Windows (in Windows 10) instead of Cygwin and then installed dos2unix using sudo apt-get install dos2unixand used the following command to fix this problem:
$ dos2unix < compilelibs.sh > output.sh
I am trying out a simple bash script using for loop, and kept getting the following error:
'/test.sh: line 2: syntax error near unexpected token `do
'/test.sh: line 2: `do
The following is the code that is being used...
for animal in dog cat elephant
do
echo "There are ${animal}s.... "
done
However, when I tried on other machines.. it is working no problem.
Please help.
Your test.sh script has Windows-style line endings. The shell sees each \r\n sequence as a \r character at the end of the line.
The shell is seeing do\r rather than do. Since \r sends the cursor to the beginning of the line, that's why you're seeing the quotation mark at the beginning of the line. Try
./test.sh 2>&1 | cat -A
to see what's actually in the error message.
Filter your test.sh script through something like dos2unix to correct the line endings. (Be sure to read the man page first; dos2unix, unlike most text filters, overwrites its input file.)
This is a common problem on Cygwin. Did you use a Windows editor to create the script?