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.

3
  • 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? Commented Mar 30, 2018 at 0: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 Commented Mar 30, 2018 at 0: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 Commented Mar 30, 2018 at 0:16

2 Answers 2

1

I could do it with a workaround:

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

MOBILE=$(pwd)
Sign up to request clarification or add additional context in comments.

6 Comments

If you are doing pwd and doing ls on it then what is the use of that cd command here then?
This ls was just an example, after setting the MOBILE variable I have other commands, I edited my answer thanks for poiting it out
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.
You can quote only part of a string. ls "/run/user/${UID}/gvfs/mtp"*"/Internal shared storage/" quotes everything except the *.
Also consider using "$PWD" rather than $(pwd) -- much more efficient.
|
-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.

2 Comments

I believe OP already mentioned OP has tried this already.
Thanks for your reply! I did that but I still got the error because of the wildcard *, but I figured out with a workaround (posted and answer) :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.