4

I'm trying to run linux shell script on adb shell. It's giving errors! Here is the whole story:

I wrote a simple bash script hello.sh :

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

hello

running it as ./hello.sh produces the o/p

hello world!

Now I pushed the file to android device using

adb push hello.sh /data/folder_name

then ran following command to enter in adb shell

adb shell

In adb shell fired following commands

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 happening here! Or is there some different way to write function for adb shell script

I searched but didn't get proper solution Please help.

Vikram Dattu
  • 801
  • 3
  • 8
  • 24

3 Answers3

4

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; }
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

When invoked as sh, bash enters posix mode and it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

The reserved word function is optional for bash, but I think is unknown to historical versions of sh.

Try to invoke the command as

bash /tmp/test.sh
MaxChinni
  • 1,206
  • 14
  • 21
0

You don't need to push the script to your phone - simply expand it in the shell itself like so and you save yourself time:

adb shell "$hello.sh"
Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55