Shell Script to remove lines of text in a text file - android

How can I remove a line of text in a file if it exists?
So far I am guessing
#!/sbin/sh
mount -o remount,rw /system;
# Make a backup first
cp /system/build.prop /system/build.prop.bak;
# Append
if [ grep -o 'wifi.supplicant_scan_interval' <<</system.build.prop > 1 ]; then
echo "YO";
fi;
mount -o remount,ro /system;
however, this shows me YO no matter of it is > 1 or < 1 (it does exist in the file), so this part seems wrong, also, I don't know how I could remove the line?
Can you help?
Code Update
#!/sbin/sh
mount -o remount,rw /system;
function check_prop(){
busybox grep $1 /system/build.prop;
return $?;
}
# Make a backup first
cp /system/build.prop /system/build.prop.bak;
echo $(check_prop 'wifi.supplicant_scan_interval');
# Append
if [ $(check_prop 'wifi.supplicant_scan_interval') > 1 ]; then
# Do my stuff here?
echo 'YO';
fi;
mount -o remount,ro /system;
is returning me a blank line, and YO. If I change it to < 1 it does the same thing

sed '/wifi.supplicant_scan_interval/d' inputfile
would remove lines matching wifi.supplicant_scan_interval
eg
$cat input
hello
world
hai
$sed '/world/d' input
hello
hai
if you want to delete line from file use -i option which is does the action inplace
sed -i '/wifi.supplicant_scan_interval/d' inputfile
EDIT
using grep to print all lines except the lines that match the patter.
grep -v 'wifi.supplicant_scan_interval' inputfile
eg
$ grep -v 'world' input
hello
hai
the -v option does the negation.

Related

Get File Permissions and Parent Directory

How can I get the $DIR and $OPERM to return the correct values?
I am attempting to grab all sqlite3 databases and while the all list if I echo $i;, I need to work on:
the parent folder (in order to do what I need with $i)
store the original file permissions for later use on said file.
So far, all I've managed to do is get this to echo the words sed and stat
for i in $($BB find /system -iname "*.db")
do \
ORPERM=(stat -c "%a" $i);
DIR=(sed 's|/[^/]*$||' $i);
echo $DIR;
echo $OPERM;
done;
p.s. $BB = busybox
You missed the $ sign:
ORPERM=$( stat -c "%a" $i );
DIR=$( dirname $i );

String equality in Bash script [duplicate]

This question already has answers here:
How to compare strings in Bash
(12 answers)
Closed 8 years ago.
I want to simply test if an android device is rooted from a computer (for a root/backup/flash script).
So i have to test some locations for the su binary. Here is my code :
#!/bin/sh
#####################
## ROOT CHECK
SU_LOCATIONS="/system/bin/su /system/xbin/su"
check() {
echo "Checking for SU binary…"
for file in $SU_LOCATIONS
do
suFileResult=`$ADB shell "ls $file"`
echo "suFileResult = $suFileResult"
echo "file = $file"
if [ "$suFileResult" == "$file" ];
then echo "Su trouvé à $suFileResult"
fi
done
echo "$suFileResult" > tmpbak/suFil
}
The problem is that even if file == suFileResult, "if" return false. If i remove the spaces around ==, "if" will always return TRUE…
What am i doing wrong ? If you give an other way to test the file (and where), it would be perfect.
Thanks for your answers !
PS : the string could contain spaces here, such as :
/system/bin/su: No such file or directory
EDIT After answer :
By the way, i figured how to remove this annoying \r character : add
| tr -d '\r'
will remove this character in ALL the string (not only on the end). So in my case :
suFileResult=$(adb shell "[[ -e $file ]]; echo \$?;" | tr -d '\r')
By some weird reason adb returns strings with DOS line-ending (\r\n).
$ suFileResult="$(adb shell "ls $file")"
$ set | grep suFileResult
suFileResult=$'/system/bin/su\r'
So the proper condidtion statement may look like:
[[ $suFileResult == ${file}$'\r' ]]
Also, I should note that using ls for checking file existence is a quite odd approach, even though adb does not return an error code, you could print it to STDOUT:
suFileResult=$(adb shell "[[ -e $file ]]; echo \$?")
if [[ $suFileResult == $'0\r' ]]; then
echo "Su trouvé à $file"
fi

Shell script search & replace in text file

I have to edit text file in android shell.
so I type this shell script.
but my Galaxy Nexus does not have Sed, Awk neither.
shell#android:/ $ sed -e "s/old_pattern/new_pattern/g" file_name > modify_file
/system/bin/sh: sed: not found
it doesn't worked.
how can i modify old_pattern to new_pattern in text file.
is it possible in Shell Script?
Edited Shell Script
#!/system/bin/sh
ARGS=4
BAD=65
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` TARGET_FILE,OLD_PATTERN,NEW_PATTERN,MODIFY_FILE"
exit $BAD
fi
old_pattern=$2
new_pattern=$3
modify_file=$4
if [ -f "$1" ]
then
target_file=$1
else
echo "\"$3\" Does not exist."
exit $BAD
fi
exit 0
Solution :
shell#android:/ $ while read STRING
>do
>echo "${STRING//old_pattern/new_pattern}" >> modify_file_name
>done < target_file_name
shell#android:/ $
It is possible with pure bash, but it will be very slooow on big files.
while read STRING
do
echo "${STRING//old_pattern/new_pattern}" >> modify_file
done < file_name
ps. Oh, I just mentioned that your shell isn't bash. Seems like it is just sh. That won't work with sh.

How to get android application name from apk file

How do I get the android application name from a apk file programatically outside an android environment? I have tried parsing androidmanifest.xml but it only shows the package name ( which may not be very informative at times)
If you are using Linux, this command will give you the application name:
aapt dump badging your.apk | sed -n "s/^application-label:'\(.*\)'/\1/p"
You'll have to install aapt first.
Can also be done with apktool.
#!/bin/sh
if [ -z "$1" ]; then echo ${0##*/} useage: ${0##*/} /path/to/your.apk; exit; fi
APKTOOL=$(which apktool apktool.sh | head -1); if [ -z "$APKTOOL" ]; then echo "Error: Could not locate apktool wrapper script."; exit; fi
TMPDIR="/tmp/apktool"
rm -Rf $TMPDIR
$APKTOOL d -q -f -s --force-manifest -o $TMPDIR $1
LABEL=$(cat $TMPDIR/res/values/strings.xml | grep 'string name="title"' | sed -e 's/.*">//' -e 's/<.*//')
rm -Rf $TMPDIR
echo ${LABEL}

adb push/pull with progress bar

It is really annoying if you adb push/pull large files to the device that there's no way to now how far along it is. Is it possible to run adb push or adb pull and get a progress bar using the 'bar' utility?
The main issue here is I think that adb expects two file names, if the input file could be replaced by stdin you could pipe through the 'bar' utility and get a progress bar. So far I haven't succeeded in doing so, but I'm not really a shell guru which is why I'm asking here :)
Note that I'm on Linux using bash.
It looks like the latest adb has progress support.
Android Debug Bridge version 1.0.32
device commands:
adb push [-p] <local> <remote>
- copy file/dir to device
('-p' to display the transfer progress)
However, the answers above also work for 'adb install' which do not have a progress option. I modified the first answer's script to work this way:
Create "adb-install.sh" somewhere in your PATH and run "adb-install.sh " instead of "adb install -f "
#!/bin/bash
# adb install with progressbar displayed
# usage: <adb-install.sh> <file.apk>
# original code from: http://stackoverflow.com/questions/6595374/adb-push-pull-with-progress-bar
function usage()
{
echo "$0 <apk to install>"
exit 1
}
function progressbar()
{
bar="================================================================================"
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 1 ]] && usage
SRC=$1
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
export ADB_TRACE=all
adb install -r $SRC 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
export ADB_TRACE=
echo
echo 'press any key'
read n
Currently I have this little piece of bash:
function adb_push {
# NOTE: 65544 is the max size adb seems to transfer in one go
TOTALSIZE=$(ls -Rl "$1" | awk '{ sum += sprintf("%.0f\n", ($5 / 65544)+0.5) } END { print sum }')
exp=$(($TOTALSIZE * 7)) # 7 bytes for every line we print - not really accurate if there's a lot of small files :(
# start bar in the background
ADB_TRACE=adb adb push "$1" "$2" 2>&1 | unbuffer -p awk '/DATA/ { split($3,a,"="); print a[2] }' | unbuffer -p cut -d":" -s -f1 | unbuffer -p bar -of /dev/null -s $exp
echo # Add a newline after the progressbar.
}
It works somewhat, it shows a progress bar going from 0 to 100 which is nice. However, it won't be correct if you do a lot of small files, and worse, the bytes/s and total bytes shown by 'bar' aren't correct.
I challenge you to improve on my script; it shouldn't be hard! ;)
Here is my solution, it will show a simple progressbar and current numeric progress
[==================================================] 100%
Usage
./progress_adb.sh source destination
progress_adb.sh
#!/bin/bash
function usage()
{
echo "$0 source destination"
exit 1
}
function progressbar()
{
bar="=================================================="
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 2 ]] && usage
SRC=$1
DST=$2
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
ADB_TRACE=adb adb push $SRC $DST 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
echo
Testing on Ubuntu 14.04
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
TODO
directory support
progressbar size change when screen size change
Well I can give you an Idea:
ADB_TRACE=adb adb push <source> <destination>
returns logs for any command, so for example the copy command, which looks like:
writex: fd=3 len=65544: 4441544100000100000000021efd DATA....#....b..
here you can get the total bytes length before, with ls -a, then parse the output of adb with grep or awk, increment an interneral counter and send the current progress to the bar utility.
When you succeeded, please post the script here.

Categories

Resources