Bash to sh conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to sh conversion
# 1  
Old 03-04-2014
Bash to sh conversion

Code:
declare -i DEFINT=1
declare -i DEFDELAY=1
declare -i timeout=DEFTOUT
declare -i interval=DEFINT
declare -i delay=DEFDELAY

if (($# == 0 || interval <= 0)); then
    printUsage
    exit 1
fi

(
    ((t = timeout))

    while ((t > 0)); do
        sleep $interval
        kill -0 $$ || exit 0
        ((t -= interval))
    done

    kill -s SIGTERM $$ && kill -0 $$ || exit 0
    sleep $delay
    kill -s SIGKILL $$
) 2> /dev/null &

this works quite well if the first line of the script is: #!/bin/bash.

but i need this to run in a sh script, a script whose first line is: "#!/bin/sh"

how can the above be written in sh so it passes?

I tried the following and it failed miserably:

Code:
DEFINT=1
DEFDELAY=1
timeout=${DEFTOUT}
interval=${DEFINT}
delay=${DEFDELAY}

if [ $# -eq 0 ] || [ ${interval} -le 0] ; then
    echo "ERROR: WRONG USAGE!"
    exit 1
fi

t=${timeout}

    while (($t > 0)); do
        sleep $interval
        kill -0 $$ || exit 0
        (($t -= $interval))
    done

    kill -s SIGTERM $$ && kill -0 $$ || exit 0
    sleep $delay
    kill -s SIGKILL $$

# 2  
Old 03-04-2014
In what way did it fail miserably? What did it do?

Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums
# 3  
Old 03-04-2014
For starters, this should get you going. It is a rough direct translation to Posix shell, which should be what /bin/sh is on most modern systems, except Solaris 10 and earlier. There you could use /usr/xpg4/bin/sh instead...

What is the target OS?
Note that the "printUsage" function is not defined in your bash snippet.

Code:
DEFINT=1
DEFDELAY=1
timeout=DEFTOUT
interval=DEFINT
delay=DEFDELAY

if [ $# -eq 0  ] || [ $interval -le 0 ]; then
    printUsage
    exit 1
fi

{
    t=$timeout

    while [ $t -gt 0 ]; do
        sleep $interval
        kill -0 $$ || exit 0
        t=$((t - interval))
    done

    kill -s SIGTERM $$ && kill -0 $$ || exit 0
    sleep $delay
    kill -s SIGKILL $$
} 2> /dev/null &


Last edited by Scrutinizer; 03-05-2014 at 12:23 PM..
# 4  
Old 03-04-2014
You could use let to assign variables as Integer, (but there will be no testing on their value...)
Code:
let DEFINT=1
let DEFDELAY=1
let timeout=${DEFTOUT}
let interval=${DEFINT}
let delay=${DEFDELAY}

and
Code:
if [ $# -eq 0 ] || [ ${interval} -le 0]

in pure old sh (Bourne) would look more like:
Code:
if [  \ ( $# -eq 0 \) -o \(${interval} -le 0 \) ]
then
...

# 5  
Old 03-05-2014
Maybe I am missing something but exactly what shell is your /bin/sh and what OS are you running?
# 6  
Old 03-05-2014
Old Unix shell does not have Posix $(( )), needs `expr ...`
Code:
#!/bin/sh
DEFINT=1
DEFDELAY=1
timeout=DEFTOUT
interval=DEFINT
delay=DEFDELAY

if [ $# -eq 0 -o $interval -le 0 ]; then
    printUsage
    exit 1
fi

(
    t=timeout

    while [ $t -gt 0 ]; do
        sleep $interval
        kill -0 $$ || exit 0
        t=`expr $t - $interval`
    done

    kill -s SIGTERM $$ && kill -0 $$ || exit 0
    sleep $delay
    kill -s SIGKILL $$
) 2> /dev/null &

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Scripting with date format conversion

I have a script below and wanted to change the output into three different file format (3 separate script) #!bin/bash #input file format postwrf_d01_20131206_0600_f08400.grb2 #postwrf_d01_YYYYMMDD_ZZZZ_f0HHHH.grb2 #zzzz= 0000,0600,1200,1800 (in UTC) #HHHH=00000,00600,01200,01800 ..ect (in... (1 Reply)
Discussion started by: cumulus_255
1 Replies

2. Shell Programming and Scripting

Batch to bash conversion

Hi, I am just trying to convert the batch script to bash script and i am stuck at one point where I have the below code for /f "delims=" %%a in (a.txt) do ( for /f "tokens=1,2,3* delims==" %%i in ("%%a") do ( for /f "tokens=1,2,3* delims= " %%x in ("%%i") do ( if... (4 Replies)
Discussion started by: prasanna2166
4 Replies

3. Shell Programming and Scripting

Different epoch conversion result for bash and csh users

Hi there I'm using this script to convert command line history with Epoch time stamp to human readable. While it works fine with users with /bin/csh shell, it fails to convert for users with /bin/bash shell. Why is this happening? I even changed and added * and after the # but it still didnt... (2 Replies)
Discussion started by: hedkandi
2 Replies

4. Shell Programming and Scripting

Error with Audio Conversion Bash Script

Good evening, I'm currently working on a BASH script to convert audio between file formats and I've come across a snag. At the beginning of the script, I'm having the system check to see if any files with the .m4a extension exist in the directory, and if so, it runs the script. If there are no... (1 Reply)
Discussion started by: KBurkholder
1 Replies

5. UNIX for Advanced & Expert Users

.so to .sl conversion ?

Hi all, I have one libxxx.so file ( which I got from a third party ). We use shared library libxxx.sl . Is there any way to convert the .so file to .sl file ? Thanks in advance - M (3 Replies)
Discussion started by: kanu_kanu
3 Replies

6. Shell Programming and Scripting

Typeset conversion problem from ksh to bash

Hi, typeset -l sgf # all lowercase letters typeset -u SGF # all uppercase letters sgf=$1 SGF=$sgf these lines used in my scripts . It ran fine in ksh but when we convert this to bash it erroring out. I like to know what the use of typeset ?? Thanks & Regards kanagaraj (3 Replies)
Discussion started by: kanagaraj
3 Replies

7. UNIX for Dummies Questions & Answers

BASH uppercase conversion

How can I convert a variable to uppercase like ksh, typeset -u $1 in bash? (1 Reply)
Discussion started by: stringdom
1 Replies

8. UNIX for Dummies Questions & Answers

Another bash shell to perl conversion

Hello everyone. I am new to linux and need help again. I need help converting this bash shell to linux: for i in `ls -l *.txt` do `./cnvidtf.pl $i` `curl -u login:pswd --disable-espv -T loadfile.seq ftp://11.1.11.1` `mysql -u login -h 11.1.11.1 -ppswd < lddocs.sql` done Thanks! Any help... (6 Replies)
Discussion started by: freak
6 Replies

9. UNIX for Advanced & Expert Users

conversion

Dear friends, i am writing sh shell script I have a file containing binary data. like this. 010101010101010101101010101010100001010101010101001. i want to read some particular bits and convert it into decimal valuse. example. 1.first i want to read 5 bits and convert it into... (1 Reply)
Discussion started by: rajan_ka1
1 Replies

10. Shell Programming and Scripting

Conversion of bash parsing script to perl?

I need help with a perl parsing script. I have some error logs on a windows machine that I need to parse from a text file, but I know nothing about perl. I usually run this bash script on my linux box and it does just what I need. How would I do the same thing with perl and port it to my windows... (2 Replies)
Discussion started by: cstovall
2 Replies
Login or Register to Ask a Question