Run linux bash script on adb shell

I am trying to run a linux shell script in adb shell. It gives errors! Here's the whole story:

I wrote a simple bash script hello.sh:

#!/bin/bash
function hello
{
    echo "hello world!"
}

hello

      

runs it like. /hello.sh, issues the o / p command

hello world!

      

Now I have pushed the file to my android device with

adb push hello.sh /data/folder_name

      

then ran the following command to enter in adb shell

adb shell

      

The following commands run in adb shell

cd /data/folder_name
chmod 755 hello.sh
sh hello.sh

      

This is what I get on adb shell:

# sh hello.sh
sh hello.sh
function: not found
hello world!
hello: not found
#

      

What's going on here! Or is there another way to write a function for adb shell script

I searched but didn't get the right solution Please help.

+3


source to share


2 answers


Not sure about adb, but "function" is not standard syntax. It is available in many shells, but the standard way to define a function is:



hello() { echo hello world; }

      

+4


source


When called sh

, bash goes into posix mode, and it tries to mimic the startup behavior of historical versions of sh as closely as possible, while still conforming to the POSIX standard.

The reserved word function

is optional for bash, but unknown to historical versions I guess sh

.



Try calling the command like

bash /tmp/test.sh

      

0


source







All Articles