Is this shell script correct? - android

I have a shell script like this :
sed -i '/^###########/,/^#End of Build.Prop/d' /system/build.prop;
#
sed -i '/^#Start Build.Prop Tweak/,/^#End of Build.Prop Tweak/d' /system/build.prop;
#
sed -i '/^#Start Build.Prop Tweak/,/^ro\.config\.hwfeature_wakeupkey=0/d' /system/build.prop;
Of the three Shell Commands stated above none of them works when put in a sh file. But, if I use a TerminalEmulator, the three scripts can be executed
I want to use the scripts in an Android Device

No, it is too dangerous.
When the end-search tag is missing, you will delete a large part of your file.
When you want to delete the first and second line in a file, it seems working ok:
$ cat test.txt
first line
second line
third line
$ cat test.txt | sed '/first/,/second/ d'
third line
EDIT: One command less with sed '/first/,/second/ d' test.txt
But what happens when the second line can not be found?
Your sed command should skip removing lines, but it will:
$ cat test.txt | sed '/first/,/mistake/ d'
$
EDIT: One command less with sed '/first/,/mistake/ d' test.txt
All lines from the first match have been deleted !

Related

Loop for replace string by other one in different file

I have two lists
list1:
A:1
B:3
C:1
D:5
list2:
1:blue
3:green
5:red
How can i do for have something like:
Desired output(file3):
A:blue
B:green
C:blue
D:red
And here is my unworking code ...
#!/system/bin/bash
list1=$(cat file1)
list2=$(cat file2)
for i in "$list1"; do
num_file1=$( echo $i | cut -d ":" -f 2)
string_file2=$(cat $list2 | grep "$num_file1" | cut -d ":" -f 2)
echo -e "$i" | sed "s/$num_file1/$string_file2/" > list3
done
I also tried
sed 's/"$num_file1"/"$string_file2"/' and many other but failed every times for what i want .. Where i am wrung with sed ??
Ps: its on android ... and few command are misted ...
Give this awk line a try:
awk -F':' 'NR==FNR{k[$1]=$2;next}{print $1 FS k[$1]}' f2 f1
I didn't test the code, but it should work.
Note this assumes that all idx in file1 we have corresponding entry in file2
The awk solution is perfectly fine. Using lesser commands, you could say:
sort -t \: -k 2 list1 | join -t \: -1 2 - list2 | cut -d \: -f 2,3 | sort -t \:
Where i am wrung with sed ??
You were quite close, wrong only in three places.
for i in "$list1"; do has to be for i in $list1; do without quotes, since you want to process the lines individually, not the whole $list1 at once.
string_file2=$(cat $list2 | …) has to be string_file2=$(echo "$list2" | …) with echo instead of cat (since you already read the file2 into the variable $list2) and with quotes to preserve the line separation.
The output redirection > list3 has to be moved away from the echo -e "$i" | sed … line to the end of the done line, otherwise only the last output line would remain in file3.
Another approach to the problem would be to use an array for the colors, indexed by their number:
#!/bin/bash
eval rgb=($(sed 's/\(.*\):/[\1]=/' <file2)) # change 1:blue to [1]=blue etc.
while IFS=: read letter number
do echo $letter:${rgb[$number]} # change A:1 to A:blue etc.
done <file1 >file3

Bash: find and replace text from a script

I need to modify a number into a file using a bash script
I want to remove a line that contains dalvik.vm.heapsize=256 and replace it with a new line that contains dalvik.vm.heapsize=512. I not know the line number and 256 is an ipotetic value. How I can build a script that perform this action??
Use the sed command:
sed -i -e 's/^\(dalvik.vm.heapsize=\).*/\1512/' build.prop
The command searches for a line starting with dalvik.vm.heapsize=, then replaces the part after = with 512. The left part is captured using the regular expression group, and \1 in the replacement part (\1512) refers to this group. The -i option instructs to replace in-place. Refer to info sed for details.
Here is a more advanced example taking into account possible leading spaces/tabs and making the match stricter by means of the regular expression lists:
sed -i -e 's/^\([ \t]*dalvik.vm.heapsize=\)[0-9]\+/\1512/' build.prop
Perl is more flexible. I prefer to use it for more complicated tasks. There is no such simple way as sed's -i option for Perl, however; but you can simply use the shell redirection, e.g.:
cat build.prop | \
perl -n -e 's/^([\t\s]*dalvik.vm.heapsize=)\d+/${1}512/; print' > build.prop

Highlight Android NDK error output

Is there tool that adds highlighting to android ndk build output (may be on stderr).
e.g. If it will highlight word "error:" in red and "Warning:" in orange that would be what I look.
But if it will also give different colors to code and error messages then it would be awesome!
ANSWER
red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)
$ANDROID_NDK/ndk-build 2>&1 | sed -e "s/\(error:\)/${red}\1${norm}/i" | sed -e "s/\(warning:\)/${yellow}\1${norm}/i"
You could do this by piping the output of your build command (or any other terminal command) to sed:
red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)
make | sed -e "s/\b\(error:\)\b/${red}\1${norm}/i" | sed -e "s/\b\(warning:\)\b/${yellow}\1${norm}/i"

Check if line exists in a file

The problem is that my app tries to add a line to an existing xml file in /system/csc.
I need a function to check if inside the file there is X line and if it is in there the line shouldn't be added also it has to put the line before another line that is present in the file.
You can check whether the line already exist with grep and use sed to insert it if not:
grep -Fxq "foobar line" file || sed -i '/^context line$/i foobar line' file
This will insert the line foobar line before the line context line if foobar line isn't already found in the file.
Alternately you could do it all with sed:
sed -i '/^foobar line$/d;/^context line$/i foobar line' file
try
grep -F "$yourLine" file.xml
awk sum up multiple files show lines which does not appear on both sets of files
check my answer on this page (the last answer to my own question) I am using ed and after finding the line that the entry needs to be added to I use typical vi commands to add the entry after the line number.
You can use the same script after doing a grep for string in your file if not
grep pattern $file > /dev/null
if [ $? = 0 ]; then
echo "found"
else
run_ed_function
fi
To check if string equals ENTIRE LINE
FILE="./myfile"
STRING="myString"
if grep -x ^$STRING.$ $FILE; then
echo 'Found'
else
echo 'Not Found'
fi
In that case, the code would match 'myString' but not 'Hello myString'

Android shell script to remove all files and folders in a directory except one

Right now I am using rm -r /blaa/* to delete all folders and files in the blaa directory. What I am looking for is a way to remove all folders and files in the blaa directory except if the folder is named abc.
Any ideas?
In Linux:
There are many ways to this; however I believe the best way to do this is to simply use the "find" tool.
find ! -iname "abc" -exec rm -rf {} \;
We can easily find and delete the every file and folder that is not named "abc".
find - to find files
! -iname - to filter files/folders, the "!" means not
-exec - to execute a command on every file
rm -rf - remove/delete files -r for folders as well and -f for force
"{} \;" - allows the commands to be used on every file
In Android:
Since you can't use "rm -rf" and when you use "rm -r" it will delete the folder "." which ends up deleting everything.
I am guessing you have "root" on your phone because you can use the "find" tool.
find ! -iname "abc" | sed 1d | xargs rm -r
find - to find files
! -iname - to filter files/folders, the "!" means not
| - pipe sends data to next command
sed - replace text/output
"1d" - removes first line when you do "find ! -iname" by itself
xargs - runs commands after pipe
rm -r - remove/delete files, "-r" for recursive for folders
Edit: Fixed and tested in Android
You can easily change this to suite your needs, please let me know if this helps!
The Adopted Solution
...and the final hoorah... This is what worked for the use case (helps to sum up the comments below as well):
find ! -iname "abc" -maxdepth 1 -depth -print0 | sed '$d' | xargs -0 rm -r;
Notes:
-depth — reverses the output (so you dont delete sub-dirs first
-maxdepth 1 — kind of voids the use of -depth, but hey... this says only output contents of the current directory and not sub-dirs (which are removed by the -r option anyway)
-print0 and -0 — splits on line feeds instead of white space (for dirs with space in the name)
sed "$d" — says to remove the last line (cause its now reversed). The last line is just a period which including would make the call delete everything in the directory (and subs!)
I am sure someone can tighten this up, but it works and was a great learning op!
Thanks again to Jared Burrows(and the Unix community in general — go team!) — MindWire

Categories

Resources