[SCRIPT] Pause command in Bash (Shell scripts) - General Topics

For those of you who write scripts in bash, I have come up with a simple quick solution to adding the "pause" command easily.
This is meant for linux users to use to make writing scripts easier
In order to do this, follow these simple steps:
1) Open Gedit
2) add the following code:
Code:
#!/bin/bash
read -n1 -r -p "Press any key to continue..." key
exit 0
This starts the script, waits for a key to be pressed, and then ends, and the script accessing it continues.
3) Now we need to save it, so save it to your desktop as
Code:
pause //WITH NO EXTENSION
4) now we need to copy it to /bin. To do this, open your teminal window with CTRL+ALT+T, then type:
Code:
cd Desktop
sudo mv pause /bin
5) Now, make the file executable:
Code:
sudo chmod +x pause
And that's it! Now, whenever you are writing scripts, just write "pause" without quotes and it will work
**NOTE**
If you are writing this for release, you cannot simply write pause in your scripts or they will fail on the user's machine (if they don't have the pause script installed). In release scripts, just write the line:
Code:
read -n1 -r -p "Press any key to continue..." key
Thanks for looking!

Related

[GUIDE] SManager - How to use Commands

Hi,
I want to learn some of the commands for use in SManager (Script Manager) for ANDROID.
https://sites.google.com/site/devwom/script-manager
Does anyone know a suitable thread with guide or commands for SManager? I tried searching the web and XDA but did not find anything myself.
Specifically I want to know the cmd line to start an app.
EDIT 1:
Spent a good few hours trying to google a solution but drawn a blank.......Maybe Shell Scripts for Linux are not same as for Android...?
I would like to be able to run a script that does the following:
Checks if a directory exists on the sdcard
then executes a command (renaming of 2 directories)
then starts a program
EDIT 2:
well a few emails with the author of SManager and I was able to write my first script to check if the app is running and then if not, to rename folders and start the other version of the app. (I have Navigon Europe as well as Navigon Australia and was looking for a solution to allow both to co exist on my One-X at the same time. As both apps share the same directory structure the issue was that starting one version would overwrite the critical files of the other).
Now with my script installed as a widget I can toggle between each version without problem.
Here is some useful information on shell language for others (credits to the author of SManager Devwom):
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
Here are some critical bits of info received during my emails with him:
Thanks a lot for your interest in SManager.
The requested commands is a mix between linux commands and android os commands.
To get info about each command you can search in google and select the preferred page.
But the commands available in each Android device depends on running rom.
To get all available commands in your device, simply execute a ls (lower case LS) in system directories, that is:
ls /system/bin
ls /system/sbin
ls /system/xbin
ls /system/usr/bin
ls /system/usr/sbin
ls /system/usr/xbin
These are the most common directories where binaries resides in android devices.
May be you also can have commands at:
ls /bin
ls /sbin
ls /usr/bin
ls /usr/sbin
usually android rooted roms have the well known busybox, to get a list all comands inside it simply execute
busybox
to get all installed packages (and running names) from your device you can execute:
pm list packages
to get info about running process
ps
or
ps aux
or
ps -aux
depending on ps version installed
If conditions are based on test bash command this command is a bit confused, because usually it is named as [, and other times it is built-in inside shell
read "test man page" to get a full list of test command flags.
To check if an app is running:
if [ -n "$(ps|grep os.tools.scriptmanager)" ] ; then
echo SManager is running
fi
To check if app is NOT running:
if [ -z "$(ps|grep os.tools.scriptmanager)" ] ; then
echo SManager is NOT running
fi
To check if a file exist:
if [ -e /sdcard/file ] ; then
echo /sdcard/file exists
fi
To check if a file exist and it is a directory:
if [ -d /sdcard/file ] ; then
echo /sdcard/file exists and is directory
fi
To rename a directory simply use mv command:
I hope you can build your script with this info.
Start app it is very easy but getting correct info to start app is a bit confused.
starting app uses "am command"
To get correct info to start app you should execute:
logcat |grep Starting
in a terminal on device
then go to launcher and launch desired app
then go back to the terminal and you will see a line with info about how to fill am command.
For example if you execute SManager
in
logcat|grep Starting
output you will see
I/ActivityManager(* 275): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=os.tools.scriptmanager/.launcherActivity } from pid 377
so to start scriptmanager you need the following command
am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n os.tools.scriptmanager/.launcherActivity
I do not know how to stop apps from command line, I usually use killall command:
killall os.tools.scriptmanager
if you do not have killall command you can use grep and awk or cut commands
kill "$(ps|grep os.tools.scriptmanager|grep -v grep|awk '{print $2;}')"
or
kill "$(ps|grep os.tools.scriptmanager|grep -v grep|cut -f5 -d" ")"
may you need adjust -f5 flag
To execute script as shortcut install SMWidgets.
Scripts usually ends with last line , ie, not special action is required, it will exit with the last executed command return code
Also you can add
exit 0
or preferred exit value, but 0 usually means OK
To end script in the middle use
exit value
where value usually it is not 0
Also it is better use mv command instead rename command.
rename command it is not linux standard, but mv command is in all linux shells.
Android shell programing it is not equals to linux shell programming but this was one of my first scripting documents many years ago and it can be used as a guide. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
Best regards,
Devwom
Thanks, this is valuable information! It's a shame this thread didn't get more replies
I've been looking to change my wallpaper by script, but so far I haven't been succesful. With your guide, I guess I'm a step closer to my goals!
Hi.
Perhaps developer of smanager or any expert is reading...
I would like to write a script for 2 purposes. Both handle with samba share running on my ubuntu.
1st situation.: I would like to move my media, download and documents files from android to the desired folder on my Ubuntu, which is shared by samba in my LAN.
The goal is to start this script with just touching a widget.
2nd situation: I would like to copy files from my samba shared folder in ubuntu to a specific folder on sdcard in android. All files in folder on sdcard should be deleted before copying. Again just by touching a widget.
Shared folder on samba are with password and rw.
Till now I am doing it manually with esexplorer...
I would like to do it with script.
Sent from my Incredible S using xda app-developers app

[UBUNTU][DEBIAN]Common Commands used in Linux

Common Commands used in Linux.
Since, Android is based on Linux, there is much possibility that there is a need to use a Linux based OS. For such use, UBUNTU
is favoured, for its easy and more user friendly.
Today, I am going to Post a few commands that are very useful to the newbies to development
I myself am new, and have faced many problems with this, so I am posting a this thread, to help all the noobs/newbies find what they need in a single place
Okay so, lets start with some basic commands, which can be classified as general commands, and each Linux user MUST KNOW ALL OF THEM !!
Code:
sudo
It simply means "Superuser do". It requires password.
Code:
sudo apt-get install APPLICATION_name
This installs the application finding it on the internet, and gives you the suitable output(If the app exists or not).
Code:
sudo apt-get remove APPLICATION_name
It Removes the application
Code:
sudo apt-get update
it updates the repositories
Code:
sudo apt-get upgrade
upgrades your installed application with their latest versions from Ubuntu repositories
Code:
killall APPLICATION_NAME
kills (terminates) an application
Code:
ps -e
displays currently running processes
Code:
kill APPLICATION_PID
kills an application; where APPLICATION_PID.
NOTE: YOU CAN FIND "PID" USING EARLIER CODE.
Code:
wget http://path_to_file.com
downloads a file from the web to current directory
Code:
cd /PATH/TO/DIR
changes current directory to DIR. Use cd to change the current directory into any dir
Code:
cd ..
Like ms-dos, goes up one directory
Code:
dir
OR
Code:
ls
lists directory content
Code:
man COMMAND
Displays manual for command.
eg: man sudo
Code:
cp ORIGINALFILE NEWFILE
Copy a file
Code:
mv SOURCE DESTINATION
Moves a file
NOTE: YOU CAN ALSO RENAME A FILE WITH THIS.
EG: mv old_filename new_filename
Code:
mkdir FOLDERNAME
Make a directory/folder
Miscellaneous: Level 2
Code:
du -sh folder name
This calculates the size of the folder
Code:
ps -aux
This shows all the running processes
Code:
chown -R User:User dir
Change owner of files and directories
Code:
chmod 777 yourScript
This makes a shell script ".sh" extension
Code:
netstat -anltp | grep "LISTEN"
See all open ports on your machine
Code:
sudo apt-get install ufw
sudo ufw allow 80/tcp
sudo ufw allow 22/tcp
sudo ufw allow 443/tcp
sudo ufw allow 21/tcp
sudo ufw enable
Ubuntu provides a uncomplicated firewall (ufw). To install it and only allow SSH, FTP, and webtraffic use the following command.
Network Commands
Code:
ifconfig
Shows the network connections
WILL UPDATE REGULARLY AS MUCH I COULD.
Code:
passwd
Run after installing Linux when you see "[email protected]:~$" to set your root pass word
Code:
adduser
Replace and run to add a new user
Code:
adduser sudo
Replace and run to add that user to sudo
Code:
sudo apt-get purge
Replace and run to purge your system of that package
Code:
apt-cache search
Replace and run to search for new packages to install
Note: "wild cards" eg "*" are acceptable
Code:
find -type f -iname 'some-compressed-file*' -print0 | xargs -0 sudo tar -vxpzf
Replace with at least the "root" directory you want to search through and Replace " some-compressed-file " (leave the single quotes ' ' and the wild card *) and run to find every file under the given directory that has that name and the pipe " | " it out to tar so that'll extract to your current directory
Note: "find" does the finding, the "-type -f" tells find to only look for files, the "-iname" tells find to search parts of file names, the ' ' around "some-compressed-file" keeps the * from doing bad things, the "print0" tells find to "scrub" for spaces and such before outputting a result, the " | " pipes the results of find to xargs, the "-0" is because of "print0" in the find side of the pipe, and "sudo tar - vxpzf" is where tar extracts the findings of find.
Code:
find $HOME -type f -iname '*zip' -print0 | xargs -0 ls
Run to find and list every zip file under the home folder
Say you want to make a file to contain some notes wile in the command line
Code:
sudo cat > $HOME/someNotes.txt <<EOF
# place a command here
echo "hello world, I update aptget"
sudo apt-get update -q
EOF
Try editing the part after $HOME; type it in or write a file that contains the combo of " cat > $HOME/someNotes.txt <<EOF " some text or commands " EOF " and you'll find making custom scripts of varying complexity to be easy.
.......
Hope some of these are also found to be useful for others.
Thanks for starting this here thread.
Hit the link in my signature for more help with Linux for new and seasoned users
Edit 08082013- added another useful tip and reformatted commands to better fit the OP's formatting.
Sent from either my SPH-D700 or myTouch3Gs
Debian Kit/QEMU Linux Install guide for all android devices that I'm writing:
http://forum.xda-developers.com/showthread.php?t=2240397
Now have working Installers for ARM Java 7 JDK + Maptools + jMonkey
Yo ! Nice Share
To delete contents of a large text file ..
Code:
cat /dev/null > NameOfTheFile
Useful variations of ls command :
Show dir content in a list
Code:
ls -l
Show dir content in a list sorted by modification time (newer first)
Code:
ls -lt
Show dir content in a list sorted by modification time (older first)
Code:
ls -lrt
Show subdirectories recursively
Code:
ls -R
If you want to show files starting with . (hidden file), you can add -a option.
More options with command :
Code:
man ls
Useful variations of grep command :
grep is used to print lines matching a pattern.
Find the entry for current user in file /etc/passwd :
Code:
cat /etc/passwd | grep $USER
Find all the entry except current user entry in file /etc/passwd :
Code:
cat /etc/passwd | grep -v $USER
Find all the numbers in file /etc/passwd :
Code:
cat /etc/passwd|grep '[0-9]*'
Add color to grep command :
Code:
cat /etc/passwd | grep --color $USER
(You can add
Code:
alias grep='grep --color=auto'
in ~/.bashrc to always have colored grep)
Useful variations of tar command :
Create archive_name.tar from dirname :
Code:
tar cvf archive_name.tar dirname/
Create archive_name.tar.gz (compressed) from dirname :
Code:
tar zcvf archive_name.tar.gz dirname/
Extract archive_name.tar.gz :
Code:
tar zxvf archive_name.tar.gz
Useful variations of mkdir command :
Create directory and subdirectories if not existing :
Code:
mkdir -p /tmp/a/b/c
(mkdir /tmp/a/b/c will fail if /tmp/a and /tmp/a/b don't exist)
Useful tip :
Use bash variable "!$" to get the value of the last argument of the last command interpreted.
For example :
Code:
mkdir /tmp/test_a
cd !$ (equivalent cd /tmp/test_a)
or,
Code:
mv /tmp/a /tmp/b
ls !$ (equivalent ls /tmp/b)

[HOW-TO] Fix when the phone stuck in boot splash after apply V6 Supercharger

You may have applied V6 Supercharger to your phone. And after reboot, you stuck at the boot splash. Reboot once and V6 Supercharger install script tell you that you aren't supercharged. Here is how to fix it. (If you get looped in boot animation, this is NOT your fix. Sorry!)
Depend on your rom, you may have to do one of following:
Open the (rooted) File Manager, open the directory /etc/init.d/ and open file 99SuperCharger. OR
Turn on ADB on your phone, use your computer to pull the file /etc/init.d/99Supercharger by running
Code:
adb pull /etc/init.d/99SuperCharger
Edit the file at the almost bottom of the file, change the following code:
Code:
if [ "`ps | grep -m 1 [a]ndroid`" ]; then HellzYeah;
else HellzYeah &; fi;
To look like this:
Code:
if [ "`ps | grep -m 1 [a]ndroid`" ] || [ $1 = "bypass" ]; then HellzYeah;
else
rm $bootloopcookie
$0 bypass &
fi;
Depend on what you have done on step 1, do the following:
If you use (root) file manager, save the file. Grant the superuser permission if needed. Then browse to /data and delete file !!SuperChargerBootLoopMessage.log. Don't worry if it's not exist.
If you use your computer to edit your file, run the following command:
Code:
adb root
adb remount
abd push /path/of/99SuperCharger /etc/init.d/99SuperCharger
adb shell
And inside the adb shell, run:
Code:
chmod 755 /etc/init.d/99SuperCharger
rm /data/!!SuperChargerBootLoopMessage.log
exit
Don't worry if file /data/!!SuperChargerBootLoopMessage.log doesn't exist.
And that's it! The V6 Supercharger bootscript is worked as it's designed to. Thanks to zeppelinrox to make this great script.
For one who want to know some technical info (and my assumption of the cause), wait me until tomorow and i'll explain how i found it. read on:
In order to run script in /etc/init.d at boot, the line like this is inserted in init.rc:
Code:
on boot
...
# Start the init.d service
exec /system/bin/sysinit
(In case of cm - for example)
But, what does the /system/bin/sysinit do? Just this:
Code:
#!/system/bin/sh
export PATH=/sbin:/system/sbin:/system/bin:/system/xbin
/system/bin/logwrapper /system/xbin/run-parts /system/etc/init.d
And this is the real source of problem. For some reason run-parts with logwrapper doesn't move on when you run a shell script function in background. If you scroll up in file 99SuperCharger, you'll found that "HellzYeah" is actually a function. And when it's execute in a boot process, "HellzYeah" will wait for boot process to continue. But run-parts (and the whole boot process) is waiting for the script to finish. You see? A deadlock!
The workaround is to execute function in the other way. What my modification do is re-executing the script with the parameter "bypass" in the background. This way, run-parts will not wait for it anymore. Then, when the script is re-executed, it will detect that it's called with the parameter "bypass". It'll run the function "HellzYeah", wait for boot process to finish, and, when the boot process is finished, supercharge your phone!
(if anyone is reading this, please inform zeppelinrox about this problem. I'm not able to post on the development forum yet.)
(This is my first post in xda. Hopefully it's helpful)

[Q] how to launch an app from the terminal, opening specified file?

From the terminal, I would like to learn how to open a file in an android app.
As an example, how would I open /storage/emulated/0/Download/test.txt in Ted lightweight text editor?
When I try this:
Code:
am start -a android.intent.action.EDIT -n fr.xgouchet.texteditor/.TedActivity -d /storage/emulated/0/Download/test.txt
I get an error that includes "Permission Denial: startActivity asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL"
I'm not sure exactly what that's about, so I try to work around it with this:
Code:
am start --user 0 -a android.intent.action.EDIT -n fr.xgouchet.texteditor/.TedActivity -d /storage/emulated/0/Download/test.txt
Now Ted editor starts, but it opens a new file and not the specified file. I tried multiple times; one time there was a message at the top saying something to the effect that the specified file could not be opened. I've also tried different test files to exclude the possibility that test.txt is corrupt.
I'm stuck. Please help!
P.S. Ted's AndroidManifest.xml is available on github. I tried posting the link but, being new to the forum, I was not permitted to do so.
------------------
OS: CM11
Device: Moto G
Relevant apps: Terminal Emulator (jackpal.androidterm 1.0.65), Ted (fr.xgouchet.texteditor 1.8.1)
1) After countless hours of trial and error, I figured it out. Entering this command in the terminal opens the file for editing in Ted:
Code:
su -c "am start -a android.intent.action.EDIT -n fr.xgouchet.texteditor/.TedActivity -d file:/storage/emulated/0/Download/test.txt"
If you are reading this thread and like to write your own shell scripts, these two commands might also come handy if your script uses both CLI and GUI utilities:
2) To see the name of the app currently in the foreground:
Code:
su -c "dumpsys window windows" | grep mCurrentFocus | cut -d'/' -f1 | cut -d' ' -f5
3) To put the terminal in the foreground again:
Code:
su -c "am start -n jackpal.androidterm/.Term"
If you know how to do 1) (see my previous post) without root privileges, please post it here.
It doesn't make sense to me that root privileges would be required to open a user-owned file in a text editor. Android is linux, after all, so I'm hoping that anything a normal user can do via the GUI can also be done from the command line.

How to properly use Terminal Emulator shortcut widget commands?

Hello!
I want to create a homescreen widget shortcut for Terminal Emulator to do the following command line:
Code:
killall -9 mediaserver
To do so, I created a sh file type with the following lines and moved it to /system dir with rwxrwxrwx permissions:
Code:
#!/system/bin/sh
su
killall -9 mediaserver
exit
exit
And then created a widget with Term shortcut with this text on argument window:
Code:
su -c "sh /system/camscript.sh"
The problem is: When I select the Terminal Icon on my homescreen, it open the terminal emulator appears
Code:
[email protected]:/ $ su -c "sh /system/camscript.sh"
[email protected]:/ #
The thing is that I want it to execute the command and exit the terminal emulator prompt by itself.
Can anyone help me?
Thanks
Please! Someone??
teforeon said:
Code:
#!/system/bin/sh
su
killall -9 mediaserver
exit
exit
And then created a widget with Term shortcut with this text on argument window:
Code:
su -c "sh /system/camscript.sh"
The problem is: When I select the Terminal Icon on my homescreen, it open the terminal emulator appears
Code:
[email protected]:/ $ su -c "sh /system/camscript.sh"
[email protected]:/ #
The thing is that I want it to execute the command and exit the terminal emulator prompt by itself.
Click to expand...
Click to collapse
Thanks for your command example. It works for me and I can live with the disadvantage that the "Terminal Emulator" does not close after command execution. In my case I need even a running "Terminal Emulator" before I can run the "Term shortcut" although the "Term shortcut" opens a "Terminal Emulator"-window (strange ).
By the way you have written "#!/system/bin/sh" . In my case it is enough to write just "#!/bin/sh"
schnick_schnack said:
Thanks for your command example. It works for me and I can live with the disadvantage that the "Terminal Emulator" does not close after command execution. In my case I need even a running "Terminal Emulator" before I can run the "Term shortcut" although the "Term shortcut" opens a "Terminal Emulator"-window (strange ).
By the way you have written "#!/system/bin/sh" . In my case it is enough to write just "#!/bin/sh"
Click to expand...
Click to collapse
Hhaha yes, I seem to have to open it too. But the command not always works for what I need anymore (reset camera on custom firmware).
I know that I'm a little late to the party but, why not use "am force-stop jackpal.androidterm" at the end of your script.
This kills the terminal emulator app.
bamoka said:
I know that I'm a little late to the party but, why not use "am force-stop jackpal.androidterm" at the end of your script.
This kills the terminal emulator app.
Click to expand...
Click to collapse
I am even later to the party LOL
Your am force command did not work for me as it says that I need special permissions to force close an app
What did work is to add a "&& exit"
e.g.
Code:
su -c "sh /system/camscript.sh" && exit

Categories

Resources