Command Line OSX: Configuring the PATH variable - bash

I've tried to set up a folder called bin in my user directory. Sadly, my first attempt of appending the new directory was incorrect as I missed a ". I tried opening up the .bash_profile to try and delete my first attempt, but due to not really knowing what I was doing when saving I get these errors when I open the bash.
Last login: Mon Dec 23 11:13:39 on ttys000
-bash: /Users/daz/.bash_profile: line 2: unexpected EOF while looking for matching `"'
-bash: /Users/daz/.bash_profile: line 3: syntax error: unexpected end of file
darryls-mini:~ daz$ cat ~/.bash_profile
PATH=$PATH:~/bin"
PATH="$PATH:~/bin"
darryls-mini:~ daz$
The first line after cat is the incorrect one. This is the result of me trying to delete the bash_profile file and re-saving it using pico ~/.bash_profile

The error is in the following:
PATH=$PATH:~/bin"
which is causing the unexpected EOF while looking for matching `"'.
Observe the quoting. You probably wanted to say:
PATH="$PATH:~/bin"

Why don't you just edit the file and delete the line? Alternatively, this command will do it for you (assuming that your ~/.bash_profile is otherwise empty, as it seems to be in your post):
echo 'PATH="$PATH:~/bin"' > ~/.bash_profile

The error message is telling you what the problem is:
unexpected EOF while looking for matching `"'
You close the quotes without opening them:
PATH=$PATH:~/bin"
Change it to
PATH=$PATH:~/bin
or
PATH="$PATH:~/bin"

Related

How to run sh file in GIT Bash

Am trying to run physusr.sh file in GIT Bash in Windows. Am trying to set java home as below in physusr.sh file.
JAVA_HOME=C:/Program Files (x86)/IBM/WebSphere/AppServer
JAVA_EXE=$JAVA_HOME/bin/java
cd /H/US_L3/MLAdminBatchLocal/original
but am facing the error when I run the file GIT Bash
./physusr.sh: line 1: syntax error near unexpected token `('
./physusr.sh: line 1: `JAVA_HOME=C:/Program Files (x86)/IBM/WebSphere/AppServer'
I have tried using double quotes, back slash but I was getting no such file or directory error. How do I make this work. Should I run this sh file using any other tool.
Most probably the '(' bracket character of '(x86)' is causing the problem. When it executes the bash file it is maybe considering it as something else but not the path. So, to solve this, tell the executor that the whole thing is a path or we can say disable the different treatment of special characters like brackets, put the path inside single quotes.
So, change it to:
JAVA_HOME='C:/Program Files (x86)/IBM/WebSphere/AppServer'
JAVA_EXE=$JAVA_HOME/bin/java
cd /H/US_L3/MLAdminBatchLocal/original

bash script error: file or directory doesnt exist

I'm using Ubuntu 14.04
This is my first time writing a bash script.
Here it is:
#!/bin/bash
${file}="/home/isra/files/mongoTemp.json"
${fdiff}="/home/isra/files/mTempDiff.json"
.
.
commands
.
.
mv ${file} ${fileold}
.
.
commands
.
.
exit
But it gives me these two errors:
./index.sh: line 4: =$HOME/files/mongoTemp.json: No such file or directory
./index.sh: line 5: =$HOME/files/mTempDiff.json: No such file or directory
mv: missing file operand
couldn't open [$HOME/files/]
Honestly, I'm not quiet sure what the problem is.. The files do exist, I've manually created them when it first complained about them but it didn't solve the problem and also, the mv command is not working. What am I doing wrong?
When assigning to variables, use foo=, and not $foo=, or ${foo}=
I.e., the lines:
${file}="/home/isra/files/mongoTemp.json"
${fdiff}="/home/isra/files/mTempDiff.json"
Should be:
file="/home/isra/files/mongoTemp.json"
fdiff="/home/isra/files/mTempDiff.json"
The rest looks fine.
Note that the two first errors are because of the ${..}=... lines.
The variables $file and $fdiff are empty, which then cause the mv error "mv: missing file operand". Some later command you haven't shown causes the final error. Though, most likely, everything will be fixed by properly assigning the variables.

Shell script syntax error: unexpected end of line

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

Bash for loop error

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?

"Command not found" when sourcing file with path variables

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.

Resources