In Java, you can call the Execcommand method to run the shell script. The following is an example:
1 .#! /System/bin/sh
2. # The simplest shell with no function
3. # It must be noted that #! Start with/system/bin/sh (with spaces)
4. # different from general Linux, if you use #! /Bin/sh will not be found (because there is no)
5. ls
Copy code
Use ADB push D: \ x. sh/data/ziyouku to upload shell in cmd
The first parameter is the local file location, and the second parameter is the remote location (that is, the location in Android). In the data directory of the root directory, ziyouku is a folder created by myself. if the folder fails, the read-only permission is required. the details are as follows:
Go to the ADB shell and run the mount command to check which blocks are mounted.
1. c: \ Documents ents and Settings \ ziyoukutang> ADB Shell
2. # Mount
3. Mount
4. rootfs/rootfs Ro 0 0
5. tmpfs/dev tmpfs RW, pattern = 755 0 0
6. devpts/dev/PTS devpts RW, mode = 600 0 0
7. proc/proc RW 0 0
8. sysfs/sys sysfs RW 0 0
9. tmpfs/sqlite_stmt_journals tmpfs RW, size = 4096 K 0 0
10./dev/block/mtdblock0/system yaffs2 Ro, noatime, nodiratime 0 0
11./dev/block/mtdblock1/Data yaffs2 RW, nosuid, nodev 0 0
12./dev/block/mtdblock2/cache yaffs2 RW, nosuid, nodev 0 0
Copy code
For example,/dev/block/mtdblock0/system is read-only, and/dev/block/mtdblock1/data is read/write.
Run the mount command to modify the read/write permissions.
For example, Mount-T yaffs2-O remount, RW, noatime, nodiratime/dev/mtdblock0/System
The-O option is used to describe the mounting method of a device or file.
Loop: used to connect a file to the system as a hard disk partition.
RO: mounting a device in read-only mode
RW: mounting a device in read/write mode
For specific usage, you can use the Google Linux Command MOUT.
The above sentence means that the/system folder has the read and write permissions.
2. Run CD to the shell folder to view the permissions of X. Sh. Generally, If you upload the file, you do not have the execution permission. You can use LS-L to view the permissions.
Use chmod 777 X. Sh to add permissions. For details, use Google CHMOD.
3. Execute./X. Sh
4. How to execute shell in a program
1. Package com. ziyouku. hellocommand;
2.
3. Import java. Io. bufferedreader;
4. Import java. Io. ioexception;
5. Import java. Io. inputstream;
6. Import java. Io. inputstreamreader;
7. Import Android. App. activity;
8. Import Android. OS. Bundle;
9. Import Android. widget. textview;
10.
11. Public class hellocommand extends activity {
12./** called when the activity is first created .*/
13. Private textview TV;
14.
15. @ override
16. Public void oncreate (bundle savedinstancestate ){
17. Super. oncreate (savedinstancestate );
18. setcontentview (R. layout. Main );
19. TV = (textview) findviewbyid (R. Id. tv01 );
20.
21.
22. Try {
23. Execcommand ("./data/ziyouku/X. Sh ");
24.} catch (ioexception e ){
25. // todo auto-generated Catch Block
26. E. printstacktrace ();
27 .}
28 .}
29.
30. Public void Execcommand (string command) throws ioexception {
31.
32. // start the LS command running
33. // string [] ARGs = new string [] {"sh", "-c", command };
34. runtime = runtime. getruntime ();
35. Process proc = runtime.exe C (command); // This is the call between Shell and advanced language.
36. // if there is a parameter, you can use another overloaded exec method.
37.
38. // In fact, a sub-process is started during this execution. It does not have a console for the parent process.
39. // No output is visible, so we need to use the output stream to get the output after shell execution.
40. inputstream = Proc. getinputstream ();
41. inputstreamreader = new inputstreamreader (inputstream );
42. bufferedreader = new bufferedreader (inputstreamreader );
43.
44. // read the LS output
45.
46. String line = "";
47. stringbuilder sb = new stringbuilder (line );
48. While (line = bufferedreader. Readline ())! = NULL ){
49. // system. Out. println (line );
50. sb. append (line );
51. sb. append ('\ n ');
52 .}
53.
54. TV. settext (sb. tostring ());
55.
56. // using exec will not be returned until the execution is successful. It will return immediately
57. // in some cases, it is terrible (for example, when copying a file)
58. // wairfor () can be returned only after the command is executed.
59. Try {
60. If (Proc. waitfor ()! = 0 ){
61. system. Err. println ("exit value =" + Proc. exitvalue ());
62 .}
63 .}
64. Catch (interruptedexception e ){
65. system. Err. println (E );
66 .}
67 .}
68 .}