I want to declare character variables and then write those variables one after the other in order to form a command. Example:
#!/system/bin/sh
tt=e;rr=c;uu=h;yy=o;
zz=i;ll=f;pp=n;cc=t
x=29
$zz$ll [ "$x"-eq 29 ]
$cc$uu$tt$pp
$tt$rr$uu$yy "yes"
$ll$zz
This code should read:
if [ "$x" -eq 29 ]
then
echo "yes"
fi
This works for the "echo" command but won't work for "if".
Always getting errors: if not found, then not found, fi not found.
I've tired surrounding with quotes and braces.
This is being done on android.
It turns out that i can achieve the desired outcome by utilizing the fact that echo will work regardless. so I echo the entire contents of the shell script in question (test.sh) and run commands in another bash instance reading from stdin.
Modified code now is
tt="e";rr="c";uu="h";yy="o";_1="i";ll="f";pp="n";cc="t"
x=29
"$1" "
$_1$ll [ "$x" -${tt}q 29 ]
$cc$uu$tt$pp
$tt$rr$uu$yy \"yes this file ran without error\"
$ll$_1
"
to run this:
/system/test.sh echo | sh -
Related
i don't know what the right title for this question but i have a problem like this:
i have a file inside a folder, the name is test.txt and have a value 6
i open the terminal and use su command to get root access (just in case), then i try this script
tek="/a/test.txt"
if [ "$tek" -le "8" ]; then
echo " 6 <= 8 "
else
echo "nice"
fi
and there is an error sush: /a/test.txt: unexpected '/' tmn
i try another way like this tek="a/test.txt" and i get an error sush: a/test.txt: zero divisor tmn
can someone tell me what is wrong with my script?
i do this all in Android device
if there is someone know what is the right title for this question please change it, i'll be so grateful
To read the first line, you have to call $(head -n 1 a/test.txt), not just reference the file path, so try this instead:
#!/bin/sh
tek=$(head -n 1 a/test.txt)
if [ "$tek" -le "8" ]; then
echo "6 <= 8"
else
echo "nice"
fi
First, sorry if my question is obscure or in an inconvenient format. This is my first post here :D.
My issue is that I have a script, let's say test.sh which reads an input, and validates if it's a positive integer (reg ex used from this post:
BASH: Test whether string is valid as an integer?):
#!/bin/sh
echo -n " enter number <"
read num
if [[ $num =~ ^-?[0-9]+$ ]] #if num contains any symbols/letters
then # anywhere in the string
echo "not a positive int"
exit
else
echo "positive int read"
fi
I am running this script on my android device (Xiaomi Mi3 w) using adb shell and the error:
syntax error: =~ unexpected operator keeps displaying.
First, is my regex even correct?
Second, any hints on how I can overcome this syntax error?
The default shell in Android is mksh. It is not 100% compatible with bash. So do not expect all bash recipes to work without changes.
For the description of features supported by mksh - read its manual page.
This is a GNU bash POSIX regular expression. In Korn Shell, you can use extglob regular expressions to the same effect:
if [[ $num = ?(-)+([0-9]) ]]; then
…
See the section “File name patterns” in the manpage for details.
I had to use ksh expression as shown below to get this to work.
case $num in
+([0-9])*(.)*([0-9]) )
# Variable positive integer
echo "positive integer"
;;
*)
# Not a positive integer
echo "NOPE"
exit
;;
esac
I have a bash script which goes as follows
if [ -f "/sdcard/testfile"]
then
echo "exists" > /sdcard/outfile
else
echo "does not exist" > /sdcard/outfile
fi
I have sufficient permission to run this with /system/bin/sh.
I am calling this script from my application and running this with /system/bin/sh.
But after running I am getting false, even if the file '/sdcard/testfile' is there.
When I am explicitly running in adb shell, I am getting this error
[: not found
Is there any other way to accomplish this task? I cannot just use java.io.File because of permission issue of application; therefore, I am adhering to shell script (command).
I need the output in the application itself. I mean,
if(filesAreAvailable)
executeSomething();
else
executeSomethingElse();
Basically I am programmatically writing this script in the /data/data/myPackageName/files directory and for calling the command:
if [ -f "/sdcard/testfile"]
as
fileWriterScript.write("if [ -f \"/sdcard/testfile\" ]\n")
When using test, you need a space after the opening bracket and before the closing bracket.
From man test:
SYNOPSIS
test expression
[ expression ]
So change:
[ -f "/sdcard/testfile"]
to:
[ -f "/sdcard/testfile" ]
If you need to use this in bash script then you can do it that way:
if [[ `adb shell ls /sdcard/path/to/your.file 2> /dev/null` ]]; then
echo "File exists";
else
echo "File doesn't exist";
fi
you could do a ls and then check the output - when it contains "No such file or directory" - the file is not there. But still IMHO you need the permission
I used this script. It's checking if a file exist on the phone.
#!/bin/bash
RESULT=$(adb shell "[ -f $1 ] || echo 1")
if [ -z "$RESULT" ]; then
echo "File exists!"
else
echo "File not found!"
fi
I made it work using another answer posted in stackoverflow. Reference
https://stackoverflow.com/a/6364244/2031060
I'm trying to get a shell script to take values. What I currently have is:
#!/system/bin/sh
echo "Enter the page numbers"
read page_no_first;
read page_no_final;
#
echo $page_no_first
echo $page_no_final
The echos are simply there for debug, and the problem is that they display as blanks.
The terminal results are as such:
scriptname
Enter the page numbers
1
1
5
5
Verbatim. That is the echo commands simply produce two empty lines.
I've found a sort of work around, and another related problem:
#!/system/bin/sh
echo "Enter the page numbers"
read page_no_first -s
read page_no_final -s
#
echo $page_no_first
echo $page_no_final
This should give me the read without it repeating the input. Instead, what it gives me is:
scriptname
Enter the page numbers
1
1
: is readonlyriptname[3]: read: -s
3
3
: is readonlyriptname[3]: read: -s
1
3
Ironically, this successfully writes the numbers to the variables, however, it a) does not give me a silent read, and b) gives me some funny error. Googling it doesn't help, since it's too vague.
Any help?
This work as expected (at least as I think it should):
#! /system/bin/sh
echo "Enter the page numbers"
echo -n "First: "
read page_no_first
echo -n "Last: "
read page_no_last
echo "First: $page_no_first Last: $page_no_last"
on android 4.2
I have this script which works on my linux machine
#!/bin/sh
c=1
if [ $c == 1 ]
then
echo c is 1
else
echo c is 0
fi
But when I use this in android as follows:
#!/system/bin/sh
c=1
if [ $c == 1 ]
then
echo c is 1
else
echo c is 0
fi
It gives an error like:
[: not found
EDIT
Is there any other logic to check the value of $c, whether it is 1 or 0 ?
Android shell have problem with [] in if so is there any other way to check the value of c ?
andriod shell sh is actually a link to busybox, and it is invoked as
busybox sh
you need setup [ applets manually
busybox ln -s /your_original_sh_path/busybox [
if you don't know where busybox is put, try list the /system/bin/sh which you give
ls /system/bin/sh
busybox which busybox
generally [ is an alias for test,
in Linux machine test is at
/usr/bin/test
and
if [ $c == 1 ]
is evaluated as
if test "$c" = 1
BUT here in android there is no test
so if with [] will not work in any case...
i will cross compile test for android and check it....!!!
Android does not provide a full UNIX environment, it is not a UNIX operating system. It has some similarities, much like how Windows also has some similarities to UNIX. Some Android devices and ROMs try to provide more of a UNIX-like environment that others, but you cannot rely on most of the standard shell scripting tools being installed if you are thinking about cross-device compatibility.
So for example, if you look at your GNU/Linux system, you can see that test and [ are actually programs. Try this: ls -l /usr/bin/[. Most Android installs do not include test or [. That means that if you want to try to do actual programming with Android's minimal shell environment, you have to use lots of odd tricks. You can install busybox to get a full UNIX shell environment, or you can even build busybox into your app. I do that when I need to include shell scripts in an app (for example, Lil' Debi and Commotion MeshTether).
Here's an example of writing a killall in Android's /system/bin/sh environment: http://en.androidwiki.com/wiki/Android_Shell_tips_and_tricks You can also use the various parameter expansions to create some logic, you can see an example of that in the Barnacle Wifi Tether scripts.
Use bash:
#!/system/bin/bash
or
#!/system/xbin/bash
You can check where your sh binary is pointing to on your Linux machine:
ls -l /bin/sh
Edit
BTW, use:
c=1
if [ $c -eq 1 ]
then
echo c is 1
else
echo c is 0
fi
Think you using the wrong arithmetic operator and there is a syntax error of a missing ";": try
[ $c -eq 1 ];
Also your location for Bash (sh) might be wrong at the top of your file:
#!/system/bin/sh
How about checking that the .sh file doesn't contain a carriage return before line feed.
Windows \r\n -> CR LF
Unix \n -> LF
use /system/bin/cmp for equality test.
if you need numerically test, substitute $(($c == 1)) with $c
#!/system/bin/sh
echo $c >/tmp/a
echo 1 >/tmp/b
if cmp /tmp/a /tmp/b
echo c is 1
else
echo c is 0
fi
I run into this issue also and found a solution (on another site)
if [[ $b -gt 0]]
then
echo 'Hooray it works'
else
echo 'still works'
fi