My working directory is /data/local/tmp.
I create and push a shell script file here named get_meminfo.sh.
The core function of it is record the RSS usage and save as a log file, like this: rss_res >> rss.log
But now I find a problem: I can run the script file when I enter the android shell, like ./get_meminfo.sh, and I can see the log file created in the path.
However when I try to run it from adb, like: adb shell /data/local/tmp/get_meminfo.sh, it will get an error: can't create file, read-only file system!
It seems that you do not quite understand the concept of the current working dirtectory.
The rss_res >> rss.log command creates rss.log in the current working dirtectory. Which in case of the following command sequence is /data/local/tmp:
adb shell
cd /data/local/tmp
./get_meminfo.sh
So rss.log gets created in /data/local/tmp which is world writable.
But in case of adb shell /data/local/tmp/get_meminfo.sh the cwd is the / root folder. Which is not writable and the command fails.
The easiest way to mitigate that is to use the absolute path for your log file inside of your script like this rss_res >> /data/local/tmp/rss.log
Related
I have a simple bash script to push an executable to the android and then remove it.
#!/bin/bash
adb push CMakeBuild_Android_armv8/Out/Release/exec /data/local/tmp/exec
adb shell rm data/local/tmp/exec
This is saved as 'adb_push.sh'. I made sure that this is an executable via chmod.
chmod +x adb_push.sh
But when I run this script in the Cygwin ./adb_push.sh, I get an error that there is no such directory.
CMakeBuild_Android_armv8/Out/Release/exec: 1 file pushed, 0 skipped. 62.5 MB/s (7302616 bytes in 0.111s)
rm: data/local/tmp/exec: No such file or directory
Is there any obvious steps that I am missing in creating a bash script or is there any error in what I am doing?
Any hint or comment would be highly appreciated.
ADB version:
$ adb version
Android Debug Bridge version 1.0.41
Version 31.0.0-7110759
Installed as C:\platform-tools_r31.0.0-windows\platform-tools\adb.exe
Disclaimer: I already tried putting the source and destination path in quotes, it did not work for me. Also tried the same with a .wav file instead of an executable and I get the same error which lead me to believe that something's not right in the bash script.
Your second command assumes the working directory is the root /. It appears this assumption is false.
The first command is succeeding. Only the second command is failing.
If adb shell doesn't start at the root (/), then the destination path you supply in the first command (/data/local/tmp/exec) is different from the path you supply in the second command (data/local/tmp/exec, note the missing slash at the beginning).
I'm gonna guess adb shell on your device starts a shell in some user's home directory, not /.
Option 1: Use absolute file path
You can fix this by giving the full absolute file path in your second command:
adb shell rm /data/local/tmp/exec
Option 2: Change directories before your command
Alternatively, you can change to the root directory before running the command. adb shell (as of version 31.0.0-7110759) does not have the ability to set the working directory, but you can do this inside the shell by adding a cd before your rm. Note that the command must now be quoted to prevent your local shell from interpreting the list operator &&:
adb shell 'cd / && rm data/local/tmp/exec'
Note: The adb shell default working directory may vary by device or ROM. On my stock Pixel 3 it does in fact start at the root:
$ adb shell pwd
/
I'm trying to install valgrind on my android device, but when i try to execute adb push Inst /
i get an error
C:\VmFiles>adb push Inst /
push: Inst/data/local/Inst/bin/vgdb -> /data/local/Inst/bin/vgdb
failed to copy 'Inst/data/local/Inst/bin/vgdb' to '/data/local/Inst/bin/vgdb': N
o such file or directory
Ofc file vgdb exists in this directory
C:\VmFiles>ls Inst/data/local/Inst/bin
callgrind_annotate cg_diff valgrind vgdb
callgrind_control cg_merge valgrind-di-server
cg_annotate ms_print valgrind-listener
after some observation i found there is no directory Inst/bin in local
so first you need to make a directory in /data/local/Inst/bin
and for that you need root access to your device because you cant access /data/ directory without root permissions
then do following steps.
first go to directory where vgdb is placed then
execute
adb push vgdb /data/local/Inst/bin/
Syntax for adb push :
adb push <PATH_OF_FILE_ON_HOST> <PATH_OF_FILE_ON_DEVICE>`
Since you are trying to push from Inst/data/local/Inst/bin/ path. Use the below command with absolute path.
adb push /full/path/to/Inst/data/local/Inst/bin/ /path/on/device/test_folder/
This should push all (both files and directory) content of the directory Inst/data/local/Inst/bin/ to /path/on/device/test_folder/
Make sure /path/on/device/test_folder/ exist, or run below command before hand.
adb shell mkdir /path/on/device/test_folder/
Use adb shell ls /path/on/device/test_folder/ to list, if your files are transferred to the device.
Also make sure you have permission to write to /path/on/device/test_folder
Note: You will usually have permission to write to /sdcard/ but again will require root permission to assign execute permission (chmod) to the transferred binary.
In this case, may need to root the device.
I have a couple of shell scripts stored in the /Scripts folder of my AppleScript application.
I can access them by setting my base path
set basePath to POSIX path of ((path to me as text)) & "Contents/Resources/Scripts/"
But I'm only able to run the script if I call the Terminal app
-- This works
tell application "Terminal"
set currentTab to do script (basePath & "install_key.sh")
end tell
-- This does not work
do shell script basePath & "install_key.sh"
The error on do shell script complains about not being able to find adb (Android Debug Bridge)
FWIW, here is the shell script in question (install_key.sh)
#!/bin/bash
#Find script directory
DIR="$( cd "$( dirname "$0" )" && pwd )"
adb push $DIR"/key-dev.txt" /sdcard/ &&
adb shell mv /sdcard/key-dev.txt /sdcard/key.txt
Problem
If I understand correctly, your main issue is that your script cannot detect and hold the presence of a specific command located on a system.
Solution
I believe the following code will be effective in helping you achieve your goal. This applescript allows you to find whether ADB is stored on a system and store it's path in a variable. You can add the variable to your path and export as others have suggested or have a look at the export process in Apple's TN2065.
If ADB is not found on a system then users can receive a prompt telling them what actions to take (if that aligns with your use-case or you could begin the install sequence for ADB). To test the behavior of the script you can simply change the adp to some other (fake) command that does not exist on your system. I've added the path to the dialog so that you can see the do shell is passing the contents of the which command into a variable.
try
set adbPath to do shell script "which adb"
on error errStr number errorNumber
-- If our own error number, warn about bad data.
if the errorNumber is not equal to 0 then
display dialog "ADB is not loaded onto system. Please load ADB and run this app again"
return 0 -- Return the default value (0).
else
-- An unknown error occurred. Resignal, so the caller
-- can handle it, or AppleScript can display the number.
error errStr number errorNumber
end if
end try
if length of adbPath > 0 then display dialog "ADB found continue processing..." & adbPath
The structure defined in the TN2065 above is essentially:
$ VAR=something; export VAR $ osascript -e 'do shell script "echo $VAR"' something
You might also want to try the administrator option when calling the shell script:
do shell script "command" user name "me" password "mypassword" with administrator privileges
The Technical Note TN2065: do shell script in AppleScript is the key reference for this kind of issues.
when you use just a command name instead of a complete path, the shell
uses a list of directories (known as your PATH) to try and find the
complete path to the command. For security and portability reasons, do
shell script ignores the configuration files that an interactive shell
would read, so you don’t get the customizations you would have in
Terminal.
First: find the full path of adb
you have to open a Terminal and issue the following command:
$ which adb
suppose the response is:
/Users/ronda/projects/android/sdk/platform-tools/adb
this means that the path of adb is:
/Users/ronda/projects/android/sdk/platform-tools
, now we have several way to address the problem, for example follow one of these two options:
Option1: Fix the AppleScript
do shell script "PATH=${PATH}:/Users/ronda/projects/android/sdk/platform-tools; export PATH; echo $PATH; " & basePath & "install_key.sh"
Option2: Fix the shell script
for example you could specify full path to the adb command in your .sh this way:
#!/bin/bash
#Find script directory
DIR="$( cd "$( dirname "$0" )" && pwd )"
/Users/ronda/projects/android/sdk/platform-tools/adb push $DIR"/key-dev.txt" /sdcard/ &&
/Users/ronda/projects/android/sdk/platform-tools/adb shell mv /sdcard/key-dev.txt /sdcard/key.txt
The simplest solution would be running the same bash configuration as your terminal application. The main difference is that Terminal uses an interactive bash and do shell script command doesn't. To run an interactive shell you can simply execute a new one with option -i (stands for interactive). When an interactive shell is opened the ~/.bashrc file is used, while non-interactive shells don't use this file.
do shell script "bash -i <<<" & quoted form of (basePath & "install_key.sh" as text)
if you don't like that you can simply execute the bashrc file or read the path variable and set it in a do shell script.
When I run below command directly on prompt, it works fine by pulling all files from emulator sdcard:
adb -s emulator-5556 pull /sdcard/.
However when I create bash file (extract.sh) with above command and run it I get following error:
remote object 'C:/Program Files (x86)/Git/sdcard/' does not exist
As can be seen it somehow adds C:/Program Files (x86)/Git before it. These are the contents of bash file:
#!/bin/bash
adb -s emulator-5556 pull /sdcard/.
Does anyone have an idea of why it works when direcly typing on prompt and not via bash file ? Thanks
Is there any reason you're not specifying the destination directory? For example, the batch command I use when pulling pictures from my phone over USB is adb pull "/sdcard/DCIM/Camera" "E:\Phone Pics\HTC DNA" which specifies both the source directory on the phone and the destination directory on my computer. As a side note, like enedil I recommend using this in a batch file when working in Windows.
Trying to copy file from device to desktop, here is a command:
adb pull sdcard/log.txt Users/admin/Desktop
But this command creates a folder Users/admin/Desktop inside platform-tools folder where adb is located. How to pull file to my desktop ?
Use a fully-qualified path to the desktop (e.g., /home/mmurphy/Desktop).
Example: adb pull sdcard/log.txt /home/mmurphy/Desktop
Judging by the desktop folder location you are using Windows. The command in Windows would be:
adb pull /sdcard/log.txt %USERPROFILE%\Desktop\
Be root, Define file on device and define new filename.
adb root
adb pull /data/data/.../databases/launcher.db launcher.db
On Windows, start up Command Prompt (cmd.exe) or PowerShell (powershell.exe). To do this quickly, open a Run Command window by pressing Windows Key + R. In the Run Command window, type "cmd.exe" to launch Command Prompt; However, to start PowerShell instead, then type "powershell". If you are connecting your Android device to your computer using a USB cable, then you will need to check whether your device is communicating with adb by entering the command below:
# adb devices -l
Next, pull (copy) the file from your Android device over to Windows. This can be accomplished by entering the following command:
# adb pull /sdcard/log.txt %HOME%\Desktop\log.txt
Optionally, you may enter this command instead:
# adb pull /sdcard/log.txt C:\Users\admin\Desktop\log.txt
List item
Use following command to pull data from ADB
adb pull data/user/0/project package name/files/.local/share/dbname C:\Users\vijayalaxmi.k
data/user/0/project package name/files/.local/share/dbname this path you will get when you debug application. i.e database path
project package name example => com.example
instead of C:\Users\vijayalaxmi.k user your own path where you want to save your file. for example, c:\documents
do adb pull \sdcard\log.txt C:Users\admin\Desktop