2

I'm trying to write a simple bash script to access my Android device when I connect it via USB, but I'm kinda new to this, so I'm having a hard time with a simple command.

What I wrote:

#!/bin/bash

MOBILE="/run/user/${UID}/gvfs/mtp*/Internal shared storage"
cd ${MOBILE}/

What I'm getting:

./mobile.sh: line 5: cd: /run/user/1000/gvfs/mtp*/Internal: No such file or directory

How can I make it understands the spaces in the path?

EDIT: I tried this way:

#!/bin/bash

MOBILE="/run/user/${UID}/gvfs/mtp*/Internal\ shared\ storage/"
CONTAINER="/media/ecrypted/"

cd "$MOBILE"

And I got:

./mobile.sh: line 6: cd: /run/user/1000/gvfs/mtp*/Internal\ shared\ storage/: No such file or directory

But if I manually run on terminal cd /run/user/1000/gvfs/mtp*/Internal\ shared\ storage/, it works.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
s3voch7
  • 143
  • 8
  • Welcome to SO. Could you please confirm if you have mounts named like `/run` or `/media` in your box. I just created a similar structure in my `/tmp` and command worked for me. Let me know on same? – RavinderSingh13 Mar 30 '18 at 00:15
  • I did a workaround and I could do that (posted my own answer), that wildcard saved me because these numbers after mtp= always change – s3voch7 Mar 30 '18 at 00:15
  • @RavinderSingh13 thanks for willing to help me, I actually did with a workaround (posted my own answer), and yes, I had `/run/user/1000/gvfs/mtp:host=%5Busb%3A002%2C017%5D/Internal shared storage` – s3voch7 Mar 30 '18 at 00:16

2 Answers2

1

I could do it with a workaround:

cd /run/user/${UID}/gvfs/mtp*/Internal\ shared\ storage/

MOBILE=$(pwd)
s3voch7
  • 143
  • 8
  • If you are doing `pwd` and doing `ls` on it then what is the use of that `cd` command here then? – RavinderSingh13 Mar 30 '18 at 00:16
  • This `ls` was just an example, after setting the MOBILE variable I have other commands, I edited my answer thanks for poiting it out – s3voch7 Mar 30 '18 at 00:22
  • ok sure, try to put `set -x` in your .profile or file wherever you are trying to put this variable and see what it says when it breaks, let us know on same too then. – RavinderSingh13 Mar 30 '18 at 00:23
  • You can quote only part of a string. `ls "/run/user/${UID}/gvfs/mtp"*"/Internal shared storage/"` quotes everything *except* the `*`. – Charles Duffy Mar 30 '18 at 00:28
  • Also consider using `"$PWD"` rather than `$(pwd)` -- much more efficient. – Charles Duffy Mar 30 '18 at 00:29
  • btw, note that all-caps names are used for variables with meaning to the shell or OS, whereas variables with at least one lowercase letter are reserved for application use. Thus, it's better to use `mobile=...` when it's a name you define yourself. See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph. – Charles Duffy Mar 30 '18 at 00:32
-1

Your error is happening at the space in ./Internal shared storage. You need to include quotes on the variable name if you want to use this method e.g.

#!/bin/bash

MOBILE="/run/user/${UID}/gvfs/mtp*/Internal shared storage"
cd "${MOBILE}/"

That should do the trick. Below is a relevant post.

BASH Script to cd to directory with spaces in pathname

EDIT:

Note that if you use this method, you do not need the backslash escapes on the string. Quotes is enough, that is why your fix above doesn't work.