Run commands in Linux shell using batch file

Source: Internet
Author: User
Tags echo command sqlite database root access


adb shell as root after device rooted


Once device rooted, we must perform "su" before we get root permission in adb shell,this are not convenient in some Situati Ons,so there has a method to get permission without perform "su".


-O remount,rw/4755 /system/bin/shexit


-rwsr-xr-x root Shell 285016 2015-02-02 20:57 SH

We set /system/bin/sh to SUID Permissonand now we can get root permission directly after ADB shell.



Maybe this method has secure issue, so is only for specific suituation used.


bat script automatically executes subsequent commands from ADB shell


@echo off
echo su > Temp.txt
ADB Shell <temp.txt
echo Other Commands >> Temp.txt
echo Other Commands >> Temp.txt
ADB Shell <temp.txt
Del Temp.txt


The issue with the "su" command of the ADB script command being interrupted


Take the device network package as an example, put the command line uniformly into a temporary file, and then batch execution.


ADB start-serveradb Push G:\cachelog\tcpdump/data/local/Tcpdump@echo offSetp=G:\cachelog\temp.txtecho su  > %p%echo chmod  755 /data/local/tcpdump >> %p%Echo RM -R/ Sdcard/capture.pcap >> %p%echo /data/local/tcpdump-p-vv-s 0 -w/sdcard/ Capture.pcap >> %p%adb Shell<%p%del%p%Pause
Run commands in Linux shell using batch file


I need to is able to run the following commands on a rooted Android phone connected to my PC:


-O rw,remount/777 /system/app/exitexit


And then I need to execute the following command on my PC:


ADB push: \bin\myapp_signed.apk/system/app/


And finally, I need to does some clean up and reboot the phone:


755 /system/app/rebootexitexit


How can I create a Windows batch file so that those shell commands is passed to the Android shell?



adb shell <command>  allows You-run a shell command on the device from your Computer.
If your  su  command supports the  -C  option, can do this:


adb shell su-c " Mount-o Rw,remount/system "  "chmod 777/system/app/"/system/app/ "  chmod 755/system/app/"-C reboot





If your sucommand doesn ' t support commands, you could make a script to leave on the device that bundles th E device side of it.



It appears that I is using a very simple version of SU which do not accept the-c argument.



I copied another su which did work.



Andyd is totally-though, so I am accepting his answer instead of mine:)


Launch a script as root through ADB


I have the created a script to mount partitions and does some stuff in my Android system. I saved the script as install.sh in The/bin folder of Android.



I want the script from ADB, which was itself called from a batch file on Windows, but it needs to be executed as RO Ot.



This works for me:



Create Myscript.bat and put into it (note the quotes around the commands to being executed in Superuser m Ode):


" su-c ' Command1; command2; Command3 ' "


Hen 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: This results in "Syntax error:unterminated quoted string"


" su-c ' ^Command1; ^^command3'"





This works:


ADB shell echo Command which needs root privileges \| Su


If You need redirection:


' echo anytext >/data/data/aforbiddenfolder/file ' \| Su





For ' copying ' a local file to an Android path needing root privileges (but alocalfile must not contain c5/>):


Alocalfile " echo ' cat ' >/data/data/aforbiddenfolder/file " \| Su





This works for me:


" su-c./data/local/tcpdump-arm-s 0-v-w/data/local/appxpress_dump.pcap "





But it does isn't work as it gives me a shell access (with root permissions) and nothing is executed.



How does you know that given root permissions?



I assume you is attempting to execute the script on a device? Has your device been rooted?



Need to give execute permissions via chmod to the file.


chmod ugo=rwx/bin/script.sh







Android ADB Commands Using Batch FileThis tip would show how to use a Batch file for ADB Commands. Preface


This tip was for Windows users, but the concept is the same for all platforms.



It'll show how to set several adb commands into one batch file.


Introduction


When developing for Android platform, sometimes your find yourself typing the same commands in the adb (Android De Bug Bridge),



Commands like granting permissions for internal files.



I find it much easier to create a batch file for each purpose and run it when necessary.



Batch files is script files that contain a series of commands that'll be executed at the command interpreter ( CMD on Windows).



Linux also have the same feature, in fact much more flexible, called shell scripting.



One of the common operations that I run constantly on my Android applications is



To pulled out my SQLite database to my PC on order to examine its structure.



So, instead of typing the commands one by one, I created a batch file and named it Db_lookup.bat.



The content of my batch file, simply type it in Notepad



and change the IT extension from txt to bat as the save, in order to run it as batch file.


CD%android_sdk%/platform-TOOLSCD%android_sdk%/platform-toolsadb shell su - C "chmod 777/data"adb shell su-C"chmod 777/data/data"adb shell su-C"chmod 777/data/data/your.packagename"adb shell su-C"chmod 777/data/data/your.packagename/databases"adb shell su-C"chmod 777/data/data/your.packagename/databases/mydb"adb Pull/data/data/your.packagename/databases/mydb C:\Users\Pavel\Desktop








Http://www.faqoverflow.com/android/5884.html


QUESTION


Is there a-on-me to run Adb shell as root without typing insu?



I want to is able to has root access without going into the shell.


ANSWER


If I ' m understanding your question correctly, you ' re asking how to get the root access automatically when you runadb shell,



So, you don ' t has to go into the shell and type tosuexecute a command as root on your phone.



This was controlled by a flag in your boot partition, which most custom ROMs has modified to allow root permission by DEFA Ult.



If you get dropped.$adb shell



Permanent options and one temporary one (temporary meaning that it doesn ' t stick if you reboot):


    1. Flash a ROM that includes the modification
    2. Create a custom boot.img and flash it to your phone

    3. Restart onadbdyour device with root permissions


Number 2 is actually not the difficult, but it can be somewhat dangerous if you ' re careless.



There is a wiki article this which explains the process and includes a couple of Perl scripts to help you.



These instructions assume a linux/unix environment.



I have the personally used these instructions on Fedora Linux and can-tell-you-they work, but I don't know about other * Nix environments like Macs.



I also do not know for any similar Windows instructions, but I could try to track some down if you are on Windows.



The exact commands would vary somewhat from phone to phone since different devices use different partition tables.



Generally speaking, though, you need-pull your current boot.img from your phone, unpack it, extract the RAMDisk, an D Find the default.prop file.



This is a plaintext file, which your need to open in a text editor and then find the line that contains the valuero.secure.



If the line says then you need to changero.secure=1it toro.secure=0.



After so you can re-pack the RAMDisk and boot.img, then flash it to your phone.



Once you reboot, you'll be greeted and a#prompt whenever you performadb shellwithout have to runsu.






Alternatively, if you is using a custom ROM but it doesn ' t has this modification,



You can just unzip the ROM and modify the boot.img that's included with it using the above steps.



Then your can zip up the ROM with the newly modified boot.img and flash the zip file as you normally would.



This probably goes without saying, but is careful when doing this.



Messing with your boot partition can quite easily wreck your phone and force your to recover via hboot.



I would highly recommend testing fastboot to makes sure you can use extended FastBoot commands and perform a recovery.



This varies a bit based on your model, but most phones has some sort of desktop software that can is run to reflash the P Hone as well.









The third option is this in many cases it may be possible to restart onadbdyour device with root privileges.



One possibilityadb rootis to the execute from a PC terminal, although this would not be on the all ROM configurations
(The ROM must be built as a "userdebug" one).



You can also try Chainfire ' s adbd insecure app.



This would not be persist across a reboot, so you would need to use either the app or again any time youadb rootrestarted your Phone.


ADBD Insecure v2.00


ADBD Insecure lets you run ADBD on root mode if your device is rooted.
(Note that if you are running a custom kernel, it's likely that it already implements this functionality)
If you is running a stock (made by the phone manufacturer) kernel on your device,
Chances is ADBD was running in "secure" mode, even if you are rooted.



This app lets your run adbd in "insecure" mode, which gives your root access in "adb shell",
Allows access to system files and directories through "adb push/pull",
And lets you run the ' adb remount ' command to make Your/system partition writable.

Of course, your device must already is rooted for the work.
It may isn't work with "s-on" devices and locked bootloaders.

This app does does any permanent changes to your device-a reboot would undo any modification.
That's why there was also the option to enable this feature at boot.

Note If you have a USB connected when the app activates or deactivates adbd root mode,
Need to unplug/replug the cable
or run "adb kill-server" on your computer before ADB would work again.






Run commands in Linux shell using batch file


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.