Re: Okay, I give up… how to connect my (Android) phone properly (MTP)?

Johnny Rosenberg gurus.knugum at gmail.com
Fri Aug 7 08:22:15 UTC 2015


Okay, here I go again whining about the script not working…

The script worked okay, but I found a few bugs. One of them was in my add
rule; I wrote a ”=” where there should have been ”==” for the ATTR{serial},
so the script was started whatever I plugged in, but that was easily fixed
once I realised my mistake.

My script has another bug that will happen if there is more than one folder
in ”/run/user”. That can happen and it happened when I switched user to my
wife's account. For a moment there was a directory called ”112”, which is
”lightdm” and it messed up my log file a bit.

So I modified my script a bit so it would only look for directories named
with a number that is appropriate for a human user. I understand that
different GNU/Linux-distributions have different values for that, so I
assign them to variables like this:
*DefsFile="/etc/login.defs"*
*MinUserID=$(grep "^UID_MIN" "${DefsFile}" | sed -r
's/[^0-9]*([0-9]{3,})/\1/')*
*MaxUserID=$(grep "^UID_MAX" "${DefsFile}" | sed -r
's/[^0-9]*([0-9]{4,})/\1/')*
In my case they are 1000 and 60000 respectively, but I understand that
UID_MIN for RedHat is 500, for instance.
Never mind, this part also works great now.

But how do I know in which of the directories (if more than one) that holds
my phone? Well, I could just test each one of them.
However, that can not be done, because the directory doesn't show up until
the script is done! So how to get around that?

I created another script that runs the first one in the background… and
that actually works, but I still have one problem:
According to my logfile, which I now fill with a lot of things for
debugging, the script finds the correct path to the phone, but my test line
still fails and I really don't understand why!

I can see the path in my log file, copy it to a terminal and run ”cd” on it
and it works, so why does my test fail in the script?

My code is a little bit messed up with a lot of printf statements (in
Swedish in some cases…), but here it is what I wrote so far. First the
rules:
*ACTION=="add", ATTR{serial}=="ZX1G428DSG",
RUN+="/usr/local/bin/CreateLinkToPhoneStarter.sh"*
*ACTION=="remove", ENV{ID_SERIAL_SHORT}=="ZX1G428DSG",
RUN+="/usr/local/bin/RemoveLinkToPhone.sh"*

I now run different scripts depending on the ACTION variable. Here's
*/usr/local/bin/CreateLinkToPhoneStarter.sh*:
*#!/bin/bash*

*/usr/local/bin/CreateLinkToPhone.sh &*

Not very complicated, it just starts my real script in the background.

Here's the */usr/local/bin/CreateLinkToPhone.sh* script:
*#!/bin/bash*

*# Create a link to the connected phone in the Media directory.*

*# Some information for the log file
————————————————————————————————————————————*
*LogFile="/var/log/LinkToPhone"*
*printf "%s: Skriptet startades.\n" "$(date +'%F %T')" >> "${LogFile}"*
*printf "BUS=%s, DEV=%s " "${BUSNUM}" "${DEVNUM}" >> "${LogFile}"* *# For
debug only*
*printf "ACTION=%s\n" "${ACTION}" >> "${LogFile}"* *# For debug only*

*# Variables
————————————————————————————————————————————————————————————————————*
*# First some basic variables that we need.*
*DefsFile="/etc/login.defs"*
*MinUserID=$(grep "^UID_MIN" "${DefsFile}" | sed -r
's/[^0-9]*([0-9]{3,})/\1/')*
*MaxUserID=$(grep "^UID_MAX" "${DefsFile}" | sed -r
's/[^0-9]*([0-9]{4,})/\1/')*
*printf "Tillåtna andvändarnummer: %s–%s\n" \*
* "${MinUserID}" "${MaxUserID}" >> "${LogFile}"* *# For debug only*
*LPath="/run/user"*
*RPath="gvfs/mtp:host=%5Busb%3A${BUSNUM}%2C${DEVNUM}%5D"*

*# Calculate User and XDG_Runtime_Dir by searching for the phone.*
*XDG_Runtime_Dir_ML=$(find "$LPath" -maxdepth 1 -type d -regex
"${LPath}/[0-9]+")*

*# XDG_Runtime_Dir_ML (ML=Multi Line) may contain several lines. Let's go
through*
*# all of them to find the phone:*
*Found=False*
*Count=0*
*while [[ $Found == "False" ]]; do*
* ((Count++))*
* for XDG_Runtime_Dir in $XDG_Runtime_Dir_ML; do*
* UserID=$(basename "${XDG_Runtime_Dir}")*
* printf "\t%s\t%s\n" "${UserID}" "${Count}" >> "${LogFile}"* *# For debug
only*
* # UserID must be numerical and within the UserID bounds:*
* if ((UserID>=MinUserID && UserID<=MaxUserID)) 2>/dev/null; then*
* printf "\t\tGodkänt UserID!\n" >> "${LogFile}"* *# For debug** only*
* Target="${XDG_Runtime_Dir}/${RPath}"*
* printf "Target=%s\n" "${Target}" >> "${LogFile}"* *# For debug** only*
* if [[ -e $Target ]]; then*
* printf "\t\t\tSökvägen hittad%c\n" '!' >> "${LogFile}"* *# These lines
are never*
* Found=True* *# **executed, but Target*
* break* *# exists, it really does!*
* fi* *# What's going on? :O*
* fi*
* sleep 1*
* done*
*done*
*printf "Skriptet avslutas.\n\n" >> "${LogFile}"* *# For debug only*
*exit 0 # This line will be replaced with more code once I get it to work
this far…*

*# Now just assign the right strings to all the other variables.*
*Media="/media/${User}"*
*LinkName="${Media}/${ID_MODEL}"*

*# Main
—————————————————————————————————————————————————————————————————————————*
*[[ -L ${LinkName} ]] && rm -f "${LinkName}"*
*ln -s "${Target}" "${LinkName}"*

*# For the log file:*
*printf " Händelse: %s\n Länknamn: %s\n  Målmapp: %s\n" \*
*  "${ACTION^}" \*
* "${LinkName}" >> "${LogFile}" \*
* "${Target}"*
*printf "Användare: %s (%s – %s)\n\n" * * \*
*$(getent passwd "${User}" | cut -d ':' -f 5 | cut -d ',' -f 1) **\*
* "${UserID}" **\*
* "${User}" >> "${LogFile}"*

This is absolutely ridiculous, I'm sure I'm just doing something really
really stupid here, but I just can't see it!
One thought that I had is that the break statement only breaks out from the
for statement, but since I set Found to ”True”, the while should end too.
And the printf statement inside the ”*if [[ ! -e $Target ]]; then … ; fi*”
is never executed, something is really, really wrong here.
The other if, ”*if ((UserID>=MinUserID && UserID<=MaxUserID)) 2>/dev/null;
then … ; fi*” works perfectly.

I have to kill the script manually every time:
*sudo killall CreateLinkToPhone.sh*

Here's a log file example after I killed the process after a few seconds:
*2015-08-05 21:43:45: Skriptet startades.*
*BUS=001, DEV=011 ACTION=add*
*Tillåtna andvändarnummer: 1000–60000*
* 1000 1*
* Godkänt UserID!*
*Target=/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C011%5D*
* 1000 2*
* Godkänt UserID!*
*Target=/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C011%5D*
* 1000 3*
* Godkänt UserID!*
*Target=/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C011%5D*
and so on.

If I copy the line ”
*Target=/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C011%5D*” to a terminal,
hit Enter and then type ”*cd $Target*”, I can browse my phone, so it's
nothing wrong with the path itself. It finds the correct path but it
doesn't know…

Anyone who can see my probably ridiculously silly mistake(s)? I give up for
tonight, trying again tomorrow. Thank you for listening.


Kind regards

Johnny Rosenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150807/8aa0bac8/attachment.html>


More information about the ubuntu-users mailing list