I'm trying to add the following alias in my .bash_profile in TextEdit in Mavericks (10.9.3) on a Mac Mini:
alias proj="cd ~/documents/google\ drive/web/projects"
I get the following errors:
line 1: unexpected EOF while looking for matching '"'
line 2: syntax error: unexpected end of file
bash gives:
alias proj='"cd'
However, if I copy & paste the exact same line from Notepad in my Windows VM into TextEdit it works..
bash now gives as expected:
alias proj='cd ~/documents/google\ drive/web/projects'
I've tried the following with no success:
Different paths with and without spaces
Swapped the Dell PC keyboard I was using for a Mac
Changed language to British English from British
Installed all updates
I can get round it using copy & paste as above, but would really like to get to the bottom of it. Any ideas much appreciated, thanks.
It is a quoting problem. Try:
alias proj="cd ~/documents/'google drive'/web/projects"
when eval-ing the alias command, the double quotes are removed.
When processing the "proj" invocation, the single quotes are removed.
Your original backslash was removed at alias evaluation time.
You could also double the backslashes:
alias proj="cd ~/documents/google\\ drive/web/projects"
Works for me. Turn on command tracing (set -x) and see what really happens.
$ cat xx ; source xx ; proj ; pwd
alias proj="cd $HOME/workspace/'google drive'/web"
/home/sciadmin/workspace/google drive/web
P.S. I sanity checked the equivalence of "$HOME" and "~" as an alias. Works fine, so replacing $HOME with ~ will work in the above example.
OK, here's the results for a symlink:
$ . xx ; cat xx ; proj ; pwd ; pwd -P
alias proj="cd ~/workspace/'google drive'/web"
/home/sciadmin/workspace/google drive/web
/home/sciadmin/workspace/foo bar/web
I think you may not be using BASH.
I had some issues with textedit and bash. Check textedit preferences 'GENERAL' and select plain text, then turn off smart quotes and smart dash.
If that doesn't do it - then, sadly, I am Sans Clue.
Related
So, im making a small script to do an entire task for me. The task is to get the output of the dmidecode -Fn into a text file and then take a part of the dmidecode output, in my example, the Address (0xE0000) as the file name of the txt.
My script goes as follows and does work, i have tested it. The only little issue that i have, is that the file name of the txt appears as "? 0xE0000.txt"
My question is, why am i getting a question mark followed by a space in the name?
#!/bin/bash
directory=$(pwd)
name=$(dmidecode|grep -i Address|sed 's/Address://')
inxi -Fn > $directory/"$name".txt
The quotes in the "$name".txt is to avoid an "ambiguous redirect" error i got when running the script.
Update #Just Somebody
root#server:/home/user/Desktop# dmidecode | sed -n 's/Address://p'
0xE0000
root#server:/home/user/Desktop#
Solution
The use of |sed -n 's/^.*Address:.*0x/0x/p' got rid of the "? " in 0xE0000.txt
A big thanks to everyone!
You've got a nonprinting char in there. Try:
dmidecode |grep -i Address|sed 's/Address://'| od -c
to see exactly what you're getting.
UPDATE: comments indicate there's a tab char in there that needs to be cleaned out.
UPDATE 2: the leading tab is before the word Address. Try:
name=$(dmidecode |grep -i Address|sed 's/^.*Address:.*0x/0x/')
or as #just_somebody points out:
name=$(dmidecode|sed -n 's/^.*Address:.*0x/0x/p')
UPDATE 3
This changes the substitution regex to replace
^ (start of line) followed by .* (any characters (including tab!)) followed by Address: followed by .* (any characters (including space!)) followed by 0x (which are always at the beginning of the address since it's in hex)
with
0x (because you want that as part of the result)
If you want to learn more, read about sed regular expressions and substitutions.
I have a written a sample script on my Mac
#!/bin/bash
test() {
echo "Example"
}
test
exit 0
and this works fine by displaying Example
When I run this script on a RedHat machine, it says
syntax error near unexpected token '
I checked that bash is available using
cat /etc/shells
which bash shows /bin/bash
Did anyone come across the same issue ?
Thanks in advance !
It could be a file encoding issue.
I have encountered file type encoding issues when working on files between different operating systems and editors - in my case particularly between Linux and Windows systems.
I suggest checking your file's encoding to make sure it is suitable for the target linux environment. I guess an encoding issue is less likely given you are using a MAC than if you had used a Windows text editor, however I think file encoding is still worth considering.
--- EDIT (Add an actual solution as recommended by #Potatoswatter)
To demonstrate how file type encoding could be this issue, I copy/pasted your example script into Notepad in Windows (I don't have access to a Mac), then copied it to a linux machine and ran it:
jdt#cookielin01:~/windows> sh ./originalfile
./originalfile: line 2: syntax error near unexpected token `$'{\r''
'/originalfile: line 2: `test() {
In this case, Notepad saved the file with carriage returns and linefeeds, causing the error shown above. The \r indicates a carriage return (Linux systems terminate lines with linefeeds \n only).
On the linux machine, you could test this theory by running the following to strip carriage returns from the file, if they are present:
cat originalfile | tr -d "\r" > newfile
Then try to run the new file sh ./newfile . If this works, the issue was carriage returns as hidden characters.
Note: This is not an exact replication of your environment (I don't have access to a Mac), however it seems likely to me that the issue is that an editor, somewhere, saved carriage returns into the file.
--- /EDIT
To elaborate a little, operating systems and editors can have different file encoding defaults. Typically, applications and editors will influence the filetype encoding used, for instance, I think Microsoft Notepad and Notepad++ default to Windows-1252. There may be newline differences to consider too (In Windows environments, a carriage return and linefeed is often used to terminate lines in files, whilst in Linux and OSX, only a Linefeed is usually used).
A similar question and answer that references file encoding is here: bad character showing up in bash script execution
try something like
$ sudo apt-get install dos2unix
$ dos2unix offendingfile
Easy way to convert example.sh file to UNIX if you are working in Windows is to use NotePad++ (Edit>EOL Conversion>UNIX/OSX Format)
You can also set the default EOL in notepad++ (Settings>Preferences>New Document/Default Directory>select Unix/OSX under the Format box)
Thanks #jdt for your answer.
Following that, and since I keep having this issue with carriage return, I wrote that small script. Only run carriage_return and you'll be prompted for the file to "clean".
https://gist.github.com/kartonnade/44e9842ed15cf21a3700
alias carriage_return=remove_carriage_return
remove_carriage_return(){
# cygwin throws error like :
# syntax error near unexpected token `$'{\r''
# due to carriage return
# this function runs the following
# cat originalfile | tr -d "\r" > newfile
read -p "File to clean ? "
file_to_clean=$REPLY
temp_file_to_clean=$file_to_clean'_'
# file to clean => temporary clean file
remove_carriage_return_one='cat '$file_to_clean' | tr -d "\r" > '
remove_carriage_return_one=$remove_carriage_return_one$temp_file_to_clean
# temporary clean file => new clean file
remove_carriage_return_two='cat '$temp_file_to_clean' | tr -d "\r" > '
remove_carriage_return_two=$remove_carriage_return_two$file_to_clean
eval $remove_carriage_return_one
eval $remove_carriage_return_two
# remove temporary clean file
eval 'rm '$temp_file_to_clean
}
I want to add to the answer above is how to check if it is carriage return issue in Unix like environment (I tested in MacOS)
1) Using cat
cat -e my_file_name
If you see the lines ended with ^M$, then yes, it is the carriage return issue.
2) Find first line with carriage return character
grep -r $'\r' Grader.sh | head -1
3) Using vim
vim my_file_name
Then in vim, type
:set ff
If you see fileformat=dos, then the file is from a dos environment which contains a carriage return.
After finding out, you can use the above mentioned methods by other people to correct your file.
I had the same problem when i was working with armbian linux and Windows .
i was trying to coppy my codes from windows to armbian and when i run it this Error Pops Up. My problem Solved this way :
1- try to Coppy your files from windows using WinSCP .
2- make sure that your file name does not have () characters
I am facing a strange issue with cd command and cmake.
cd command is not working with the paths which contain '-' minus sign in it. (unless used by tab expansion which is not desireable as path will be provided by ENV variable)
cmake issue
export $SOME_VAR=Some_value_for_this_variable
Now using this in cmake as
set (SOME_OTHER_VAR "$ENV{SOME_VAR}/SUFFIX")
above should give the output as SOME_OTHER_VAR=Some_value_for_this_variable/SUFFIX but instead it is replacing the env variable from starting and giving the output as SOME_OTHER_VAR=SUFFIXalue_for_this_variable means Some_v is replaced from starting with SUFFIX which is not expected.
Please help as i am not getting whats happening.
You're having some sort of character set issue. There are two different minus signs. The hyphen - (ASCII 45, U+002D), and the real minus sign − (U+2212). It's possible that the filename itself got the non-ASCII minus sign, which you can't easily type with your keyboard. The easiest fix would be to rename the file to the normal hyphen. Otherwise, you have to convince CMake to understand your Unicode filename. I have no idea if that's easy or hard.
I think your second problem is similar. The environment variable likely one or more non-printing characters in it, messing up the CMake variables, or at least the display. Try this: from the Linux command prompt, inspect the actual contents of the string.
echo $SOME_VAR | od -t c
For ASCII representation of everything, and/or
echo $SOME_VAR | od -t d1
for the byte contents
I have exactly the same pb :
Spaces in Cygwin/bash aliases?
i'm using rxvt in bash mode and i'm trying to setup my alias in the .bashrc
If i follow the answer of the previous post my alias look like this:
alias ma="/cygdrive/c/Program\ Files/Autodesk/Maya2011/bin/maya.exe"
but i get this :
$ ma
: No such file or directoryiles/Autodesk/Maya2011/bin/maya.exe
i also try this synthax :
alias ma="/cygdrive/c/Program Files/Autodesk/Maya2011/bin/maya.exe"
but it give me
$ ma
bash: /cygdrive/c/Program: No such file or directory
So if you guys have any idea on how to solve this, it would be great !
Cheers
sk
PS : i Edit my question because i'm not allowed to answer my own question
Thanks for your help guys !
i try to replace with the Progra~1 synthax and it doesn't work. So i was wondering if the pb comes from the space character.
so i try to create an alias to an .exe that doesn't need space character:
alias py1="/cygdrive/c/Python27/python.exe"
alias py2='/cygdrive/c/Python27/python.exe'
alias py3=/cygdrive/c/Python27/python.exe
alias py4='cd /cygdrive/c/Python27'
and i also get an error
$ py1
: No such file or directorypython.exe
$ py2
: No such file or directorypython.exe
$ py3
: No such file or directorypython.exe
$ py4
: No such file or directoryn27
so i wonder if the cygwin synthax for creating Alias is the same than the Linux one ?
or maybe there is something wrong with windows 7 x64 ? i really don't know where the problem come from ?
Two possible workarounds:
Replace Program Files in your path with Progra~1, or
Make a symlink to remove spaces: mklink /D c:\programs "c:\Program Files" (afair, mklink is only present starting from Vista)
Use an advanced editor like the free Notepad++ to modify the not DOS format .bashrc file. Don't use Microsoft's Notepad becouse interprets CRLF as a newline. While on Mac/Unix newline is just LF. if you have mistakenly converted and saved the file, you can find a copy of the original in cygwin\etc\skel.
To show the End of Line symbol used in a file, in Notepad++ use View -> Show Symbol -> Show End of Line.
The easier solution is to move Autodesk directory from C:\Program Files\ to C:\
+1 for file type to save .bashrc, .profile etc. with Cygwin: for example with TextPad be sure to select file type Unix on Save. Without it .... all sorts of bizareness.
well i reinstall cygwin and things are now going better
when i only put this
alias wp="/cygdrive/c/Program\ Files/Windows\ NT/Accessories/wordpad.exe"
it now works ! but when i enter this
alias wp="/cygdrive/c/Program\ Files/Windows\ NT/Accessories/wordpad.exe"
alias nk="/cygdrive/c/Program\ Files/Nuke6.2v1/Nuke6.2.exe"
the wp give me an error
$ wp
: No such file or directoryiles/Windows NT/Accessories/wordpad.exe
but the nk alias works
so i figure that the problem was coming from the carriage return character created by notepad, i try this :
alias wp="/cygdrive/c/Program\ Files/Windows\ NT/Accessories/wordpad.exe" nk="/cygdrive/c/Program\ Files/Nuke6.2v1/Nuke6.2.exe" ma="/cygdrive/c/Program\ Files/Autodesk/Maya2011/bin/maya.exe"
and it works perfectly !
so in my case the pb is i guess the carriage return created in notepad, it may create an hidden character that cygwin doesn't like. i'll try to edit my .bashrc in vi directly in order to see if the pb come from notepad or directly from carriage return.
it would be interesting if you check that if this syntax works on your computer
alias myalias1="/cygdrive/c/.../pgm1.exe"
alias myalias2="/cygdrive/c/.../pgm2.exe"
alias myalias3="/cygdrive/c/.../pgm3.exe"
thanks again for your support !
cheers !
i have a directory with a lot of subdirectories with a # infront of them:
#adhasdk
#ad18237
I want to rename them all and remove the # caracter
I tried to do:
rename -n `s/#//g` *
but didn't seem to work.
-bash: s/#//g: No such file or directory
Any ideas on this.
Thanks
Just use
$ rename 's/^#//' *
use -n just to check that what you think it would happen really happens.
In you example you have the clue about the wrong quotes used (backticks) in the error message
-bash: s/#//g: No such file or directory
bash is trying to execute a command named s/#//g.
No that using g (global) and not anchoring the regular expression you will replace any #, not just the one in the first position.
I don't know whether it's just a typo when you typed it here, but that "rename" command should work if:
you leave off the "-n" and
you quote the substitution with regular single-quotes and not back-quotes
The "-n" tells it to not really do anything. The back-quotes are just wrong (they mean something but not what you want here).
The problem is that you use backticks (`). You should use normal quotes:
rename -n 's/#//g' *
for DIR in \#*/
do
echo mv "$DIR" "${DIR/#\#/}"
done
I had to rename all folders inside a given folder. Each folder name had some text inside round braces. The following command removed the round braces from all folder names:
rename 's/(.+)//' *
Some distros doesn't support regexp in rename. You have to install prename. Even more, sometimes you can't install prename and you have to install gprename to have binary prename.
If you have 'prename' then just change backtick character " ` " to single quote and everything should work.
So the solution should be:
prename -n 's/#//g' *
or
prename -n 'y/#//' *