Results 1 to 9 of 9
Admittedly I do not know if this is the right place to ask this question but here it goes:
I am trying to write a script that will allow me ...
- 09-26-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 4
Bash read and ls commands not functioning
Admittedly I do not know if this is the right place to ask this question but here it goes:
I am trying to write a script that will allow me to pick from movies on a remote ssh server, very simple, though I am open to any suggestions. Unfortunately there are two distinct problems.
Now the first line comes up as:Code:#!/usr/bin/bash # Linux/UNIX box with ssh key based login enabled ssh Username\ Omitted@XXX.XXX.XXX.XXX ls "/cygdrive/d/My\ Documents/My\ Videos/Movies" echo "What movie would you like to play? Use quotation marks" read moviename ssh Username\ Omitted@XXX.XXX.XXX.XXX cat "/cygdrive/d/My\ Documents/My\ Videos/Movies/*$moviename*.avi" | /cygdrive/d/Application\ Files/MPlayer-mingw32-1.0rc2/Mplayer-1.0rc2/mplayer.exe -
: No such file or directory/d/My Documents/My Videos/Movies
Which I KNOW is not true because taking this identical command and putting it in the command line works.
The next problem is, after entering a value for the read command and pressing enter I get this:
': not a valid identifier read: `moviename
Yet strangely enough if I just type read moviename, enter a value, then type echo $moviename int he command line everything goes according to plan. Obviously the last command does nto get the right movie name.
I will admit my dirty secret is I am actually using cygwin on windows, but shouldn't it be the same issue?
- 09-26-2008 #2
Im assuming you're running the sshd service from within cygwin and not some Windows version.
One of the first things (issues) i see is that you are doing both doube-quoting AND backslash escaping the directory name. You should be running one or the other, like the following:
ls "/cygdrive/d/My Documents/My Videos/Movies"
Without the backslashes by the spaces.
- 09-27-2008 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 4
I'm sorry but I believe you are wrong. Removing the "\"s gives the following error on the ls command:
ls: cannot access /cygdrive/d/My: No such file or directory
ls: cannot access Documents/My: No such file or directory
: No such file or directoryvies
Which seems to imply very clearly that the double escaping is necessary.
Did you read the part where I mentioned I could copy and paste the exact command from the bash file into the terminal and it works?
- 09-27-2008 #4Just Joined!
- Join Date
- Sep 2008
- Posts
- 4
As a matter of fact every one of the commands in that script work perfectly fine when typed directly into the terminal. What am I missing here? I made the file, saved it as remotemovie.sh then ran it from cygwin with ./remotemovie.sh Why doesn't anything work?
- 09-29-2008 #5
Very strange. In order to test your environment i created a "My Video" directory under my C: drive (i dont have a D), placed a file called test.txt in there, and ran the following tests with the following results:
$ uname -a
CYGWIN_NT-5.1 BC430 1.5.25(0.156/4/2) 2007-12-14 19:21 i686 Cygwin
bclark@BC430 ~
$ ls /cygdrive/c/My\ Videos/
test.txt
bclark@BC430 ~
$ ls "/cygdrive/c/My\ Videos/"
ls: cannot access /cygdrive/c/My\ Videos/: No such file or directory
bclark@BC430 ~
$ ls "/cygdrive/c/My Videos/"
test.txt
bclark@BC430 ~
$ ls /cygdrive/c/My Videos/
ls: cannot access /cygdrive/c/My: No such file or directory
ls: cannot access Videos/: No such file or directory
As you can see, the escaped version works, the double quoted method works, but the double quoted AND escaped method does not work.
Did you read the part where I mentioned I could copy and paste the exact command from the bash file into the terminal and it works?
I did see that, but was unable to reproduce it in the manner that you say. I could only recreating your specific error was when i removed both the double quotes AND the escapes. Perhaps you can post (copy/paste) what you typed into your cygwin window and show us the results that you are seeing. Its difficult when we can only see one or the other as opposed to both, especially when you apparently have found something that goes against the way bash handles quotes and escapes.
- 09-29-2008 #6Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
Isn't bash usually in /bin/ ?
Code:#!/usr/bin/bash
- 09-29-2008 #7
It should be accessable from both /bin and /usr/bin. If you had the wrong directory in there then the shell wouldnt execute at all. It would give some sort of bad interpreter error and stop.
- 09-30-2008 #8Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Actually, the double quoting is needed because you are passing things from one shell to another. I will try to explain.
You type this on a local shell:
Let's analyze what happens. You are invoking ssh, and passing four arguments to it, that happens on our local shell. The four arguments are (one per line):Code:ssh login@server ls -ld "my dir with spaces"
The quotes are not passed to ssh, since they are consumed by our shell and mean "this is a single argument, and not a list of separated arguments".Code:login@server ls -ld my dir with spaces
But, once ssh is launched, then a new shell is spawned into the ssh session (which has nothing to do with out local shell). This new shell receives that whole list of arguments, but in that list there's nothing like quotes, so, the shell inside the ssh session receives the previous list as is:
No quotes: no good. The solution, you need to scape an additional pair of quotation marks on the first shell, so they reach your second shell (you get the idea how funny would it be to net 10 of these). Your method will work:Code:login@server ls -ld my dir with spaces
But you could very well scape the quotations directly:Code:ssh login@server ls -ld "my\ dir\ with spaces"
Or even you could double quote a pair of single quotes, or whatever is the correct terminology in english:Code:ssh login@server ls -ld "\"my dir with spaces\""
Three funny ways to achieve same thing. In all these cases, the shell inside ssh would receive this second list of parameters (slightly different, but much better for our purposes).Code:ssh login@server ls -ld "'my dir with spaces'"
Mmmm, I do this same thing, but I use sshfs to mount the remote volume locally, it saves me quite a lot of problems, I can just handle them as local files. Passwordless (key pair based) authentication would also be nice if you plan to be using this a lot (otherwise you are going to be typing passwords in front of occasional observers sometimes, I guess).Code:login@server ls -ld "my dir with spaces"
That produces me headache, try something simpler just for testing purposes, and see if it works, for example, create a file called "my test file.ext" in your server, put a simple text string into it, and try to cat it this way to see if that works:Code:#!/usr/bin/bash # Linux/UNIX box with ssh key based login enabled ssh Username\ Omitted@XXX.XXX.XXX.XXX ls "/cygdrive/d/My\ Documents/My\ Videos/Movies" echo "What movie would you like to play? Use quotation marks" read moviename ssh Username\ Omitted@XXX.XXX.XXX.XXX cat "/cygdrive/d/My\ Documents/My\ Videos/Movies/*$moviename*.avi" | /cygdrive/d/Application\ Files/MPlayer-mingw32-1.0rc2/Mplayer-1.0rc2/mplayer.exe -
Tip: Always use ${} if you are not sure what is going to be around a variable name. You have no guarantees that the wildcards are going to left untouch the name of your variable. And, in that case, ${} is the only thing that will let bash discern what is part of the var name and what is not.Code:TESTVAR=test ssh root@server cat "'/my test dir'/*${TESTVAR}*.ext"
I don't think that cygwin has something to do with your problem, it's just one shell communicating with another shell what should be worrying you. So, try my suggestions and see what happens.
By this moment, you might already have noticed that the bclark4444 example is completely different, because he is operating within a single shell, so, the standard quotation rules apply.
Cheers.
- 10-02-2008 #9Just Joined!
- Join Date
- Sep 2008
- Posts
- 4
Thank you for the tips, I'll try those out and let you all know how it goes.


Reply With Quote
