chmod android file system from script - android

I'm using an Android-x86 vm running in vmware fusion for some testing purposes. I am pushing files to it via adb push, however, every time I uninstall and reinstall the app, I have to execute the following:
adb shell
su
chmod 777 /my/path
exit
exit
I need to be able to script out this entire process but I'm not sure how I can manage the chmod process via some sort of script. I tried doing something like
adb shell am chmod 777 /my/path
But that doesn't work. I thought it would because I can do
adb shell am start ...
I also tried:
adb shell "su && chmod 777 /my/path && exit && exit"
which actually works, but doesn't exit the shell process. So any advice is much appreciated.

You can pass commands to adb shell, as you mentioned, though it works better if you surround the command in quotes.
Likewise, you can pass commands to the 'su' command using the -c argument. Add all of this together and it should do what you want in a nice one-liner!
adb shell "su -c 'chmod 777 /my/path'"

Related

How to write adb commands into a bat

I have my laptop connected to an android phone.
I am doing a task many times, so I wish to write a .bat file to run the commands automatically.
adb shell
cd /sdcard/speech
rm -f *
The bat file only executed adb shell, the rest codes were not executed.
I guess because the it entered the android device so the commands did not run as usual.
One solution was adb shell rm -f -r /sdcard/speech/*
What if there are more and more complicated commands?
Is there a way to do it ?
You can do your job with adb shell "cd /sdcard/speech; rm -f *".
For more complicated jobs, you can put all the commands in a Linux shell script, use adb push command to push the script to your Android device, and run the script using adb shell.
For example, put all the commands in run.sh, then issue:
adb push run.sh /data/local/tmp
adb shell "chmod +x /data/local/tmp/run.sh"
After this you can run your jobs with:
adb shell "/data/local/tmp/run.sh"
You can include the above line in a .bat file.

How to change permission of internal folder/file in android?

I wanted to get database file from the emulator. So, I tried to change the permission of package folder using adb shell. When I use the below command:
adb shell su -c chmod 777 /data/data/com.mypackage.test
It always returns:
Chmod: need 2 arguments
I have changed permission some days ago using above command. But recently I'm always getting the above warning. I have searched a lot and found large information regarding using above command but no one mentioned about this warning issue.
I have overcome this issue instead of running adb shell su -c "chmod 777 /data", I run the adb shell first and then the following command : su -c "chmod 777 /data".
I have overcome this issue : adb shell su -c 'chmod 777 /data/data/com.mypackage.test'

Have aliases in adb shell non-interactive mode

Hi I need to run things in the form of adb shell <command>
When I test everything out inside adb shell, it works because I was able to set some aliases in .bashrc.
However, when I do adb shell <command>, nothing works because .bashrc is not used when you run adb shell <command>, because it's in the non-interactive mode.
How can I work around this? Can I adb push some files to the filesystem so that the alias will be there when adb shell is run?
If your android device is rooted you can add your aliases for adb shell into the /system/etc/mkshrc file.
One way to do this is to issue several shell commands in a single ADB command. You can put them in a string, and separate them with semicolons, thus:
adb shell "alias foo bar ; export MY_VARIABLE=/path/to/somewhere ; my_executable "
The " are crucial here, make sure they are paired up correctly. You could run your .bashrc this way, thus:
adb shell "source /path/to/.bashrc ; my_executable"
You can write a bash script that sets the aliases and then executes your shell:
#!/usr/bin/bash
. $HOME/.bashrc
adb shell $#

Running multiple adb commands with python Popen or os.system

One problem with ADB is that you need multiple commands to get things done.
For example:
adb shell
su
cp /data/local/x /data/local/y
exit
adb pull /data/local/y
Can this be done using python popen and os-system? Tried the example below without success..
print 'Starting emulator...'
subprocess.Popen(['emulator', '-avd', 'testavd'])
os.system('adb wait-for-device')
os.system('Perform whatever adb commands you need')
Any pointers?
You can simply do:
adb shell su -c cp /data/local/x /data/local/y
adb pull /data/local/y
or, if you want to run more than one command (only Linux & OSX):
adb shell <<EOF
ls
date
cat /proc/version
exit
EOF

Launch a script as root through ADB

I have created a script to mount partitions and do some stuff in my Android system. I saved the script as install.sh in the /bin folder of Android.
I want to call the script from ADB, which is itself called from a batch file on Windows, but it needs to be executed as root.
The first solution I tried was to call the script using
adb shell "su -c sh /bin/script.sh"
but it does not work as it gives me a shell access (with root permissions), but nothing is executed.
I also tried to call
adb root "sh /bin/script.sh"
but I got the following error
adbd cannot run as root in production builds
I then tried to write
su -c "command"
for all the commands which need a root access in my script, but I have the same problem.
When I run the script I only obtain a root shell and nothing is executed.
If I use the first solution by hand (e.g. I call adb shell su, then my script), it works. However the whole point is to automate the process, so that adb shell can be called from another script.
Do you have any idea of how I could achieve this ?
Thanks !
This works for me:
Create myscript.bat and put into it (note the single quotes around the commands to be executed in superuser mode):
adb shell "su -c 'command1; command2; command3'"
then run myscript.bat from a DOS shell.
Note: it doesn't appear that the the DOS line continuation character (^) works in this situation. In other words, the following doesn't work for me:
adb shell "su -c '^
command1; ^
command2; ^
command3'"
This results in "Syntax error: Unterminated quoted string"
This works :
adb shell echo command which needs root privileges \| su
If you need redirection:
adb shell echo 'echo anytext > /data/data/aforbiddenfolder/file' \| su
For "copying" a local file to an android path needing root privileges (but alocalfile must not contain '):
cat alocalfile | adb shell echo "echo '`cat`' > /data/data/aforbiddenfolder/file" \| su
If you have a better way (even for su versions which don't have -c), I am interested.
This works for me:
adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"
I am not sure if I provided a solution or asked for a better one.
I wanted to run some 200 command in batch mode to be sent to adb
I followed this approach
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
and I saved them in a batch file
This command
adb shell "su -c 'command1; command2; command3'"
will not work beyond a certain max size . It did not work
error: service name too long
but it does not work as it gives me a shell access (with root permissions), but nothing is executed.
How do you know that you are given root permissions? I assume you are attempting to execute the script on a device? Has your device been rooted?
You may need to give execute permissions via chmod to the file.
chmod ugo=rwx /bin/script.sh
It appears that I was using a very simple version of su which did not accept the -c argument.
I copied another su which did work. AndyD is totally right though, so I am accepting his answer instead of mine :)

Categories

Resources