Help - Search - Members - Calendar
Full Version: #/bin/sh Scripting
The Planet Forums > System Administration > HOWTOs
joec@home
I am playing around on a Linux workstation using bash, sh and galeon just for fun to program fun scripts and games similar to use BASIC back in the old DOS days. Input is from terminal, scripts are run in sh and the output goes to the galeon web browser. (Sorry, I did not find the commands I really wanted to use Firefox!) Slowly I am remembering subroutines I used to use in basic and porting them over to sh and bash. While I am using galeon I am certain with some tweaking these can prove to be of value in other applications so I will be posting some of them here.

CODE
#/bin/sh
### Standard Functions

htm_out(){
###############################################
### Output to galeon web browser
###############################################
# $description5 Reserved for error codes
###############################################
    echo '<html><center><h3>'$tite'</h3></center>' > 6_out.htm
    echo '<br><br>'$description1'' >> 6_out.htm
    echo '<br><br>'$description2'' >> 6_out.htm
    echo '<br><br>'$description3'' >> 6_out.htm
    echo '<br><br>'$description4'' >> 6_out.htm
    echo '<br><br>'$description5'' >> 6_out.htm
    galeon -x 6_out.htm
}

get_key(){
###############################################
### Get single key input And pass to $key_input
###############################################
tput smso
tput rmso
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
key_input=`dd bs=1 count=1 2>/dev/null`
stty "$oldstty"
# echo
}

key_input_1_4_0(){
###############################################
### Get Multiple Choice Input
###############################################
# Required header get_key() htm_out()
# Required variables $test1 $test2 $test3 $test4 $test5
# Passes to $key_input
#
# description1='Example of 1 through 4 or 0'
# htm_out
# test1='1'
# test1='2'
# test1='3'
# test1='4'
# test1='0'
# key_input_1_4_0
# description1=$key_input
# htm_out
#
# description1='Example of 1 through 5'
# htm_out
# test1='1'
# test1='2'
# test1='3'
# test1='4'
# test1='5'
# key_input_1_4_0
# description3=$key_input
# htm_out
#
# description1='Example of 1 through 3'
# htm_out
# test1='1'
# test1='2'
# test1='3'
# test1='3'
# test1='3'
# key_input_1_4_0
# description3=$key_input
# htm_out
###############################################
    description5=''
    key_input='xxx'
    testx='n'
    testy='y'
    testz='xxx'
    get_key
    while [ $testx != $testy ]; do
        if [ $key_input -eq $test1 ]; then
            description5=''
            testx='y'
        fi
        if [ $key_input -eq $test2 ]; then
            description5=''
            testx='y'
        fi
        if [ $key_input -eq $test3 ]; then
            description5=''
            testx='y'
        fi
        if [ $key_input -eq $test4 ]; then
            description5=''
            testx='y'
        fi
        if [ $key_input -eq $test5 ]; then
            description5=''
            testx='y'
        fi
        if [ $testx != $testy ]; then
            description5='That is not a valid input. Try again.'
            htm_out
            get_key
        fi
    done
    description5=''
}

key_input_y_n_0(){
###############################################
### Get Multiple Choice Input Y N or 0
###############################################
# Required header get_key() htm_out()
# Passes to $key_input
# Converts Y to 1
# Converts N to 4
# Condition 0 Remains 0
###############################################
    description5=''
    key_input='xxx'
    test3='0'
    testx='n'
    testy='y'
    testz='xxx'
    get_key
    while [ $testx != $testy ]; do
        if [ $key_input = 'y' ]; then
            key_input='1'
            description5=''
            testx='y'
        fi
        if [ $key_input = 'n' ]; then
            key_input='4'
            description5=''
            testx='y'
        fi
        if [ $key_input -eq $test3 ]; then
            description5=''
            testx='y'
        fi
        if [ $testx != $testy ]; then
            description5='That is not a valid input. Try again.'
            htm_out
            get_key
        fi
    done
    description5=''
}

key_input_y_n(){
###############################################
### Get Input Y or N
###############################################
# Required header get_key() htm_out()
# Passes to $key_input
# Condition y Remains y
# Condition n Remains n
###############################################
    description5=''
    key_input='xxx'
    testx='n'
    testy='y'
    testz='xxx'
    get_key

    while [ $testx != $testy ]; do
        if [ $key_input = 'y' ]; then
            description5=''
            testx='y'
        fi
        if [ $key_input = 'n' ]; then
            description5=''
            testx='y'
        fi
        if [ $testx != $testy ]; then
            description5='That is not a valid input. Try again.'
            htm_out
            get_key
        fi
    done
    description5=''
}
joec@home
I am finding sometimes if you call random numbers from the same script multiple times that it repeats number. So I created a second file to be called from within a script

#/bin/bash
# Random Number Generator
# Example rand=`sh 4_random.sh 10`
# Will return a random number between 1 and 10 and store to $rand
range=$1
RANDOM=$$
rand=$((RANDOM%range+1))
val=$rand
echo $val
exit
Catalyst
Uhh, not actually. It's semantically correct --- you assign a random number to a variable, and re-use the variable, it's always going to be the original variable. Simply redeclaring the variable will fix that.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.