Starting a ksh shell

Discussion in 'Mac Programming' started by jamesapp, Aug 7, 2009.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    I want to run a shell script. But the script is written for a ksh shell. My question is how do I open a ksh shell? Would I just go to preferences and type /usr/bin/ksh where it says: shells open with:? The default is bash. I think I wasn't typing in the complete path, I was typing in /bin/ksh . I think that is where my problem was.
     
  2. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #2
    Tried /usr/bin/ksh with no luck. Do I have to quit terminal preferences and terminal for changes to take effect? I am trying to open a ksh shell any help would be appreciated. Wondering should the top of the terminal window say ksh if I am successful?
     
  3. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #3
    It's /bin/ksh.

    Your other choices of shell are /bin/sh, /bin/bash, and /bin/csh (alias /bin/tcsh).
     
  4. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #4
    I started a ksh shell, but I can't run the script the script is in the current directory I have tried typing scroller the name of the script and ./scroller and my computer is saying not found [no such file or directory] I remember having this script work, but don't know what I am doing wrong. Can I run a ksh shell script in a bash shell? Why can't I run this script in a ksh shell? Any help would be appreciated.
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    You would run the script with a non-interactive shell... You don't have to be inside ksh to do this. For example, if you are in bash:

    # ksh my_script.sh

    This assumes that ksh is in your path...
     
  6. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #6
    I'm sure /bin is in your path, but in case it's not you can use this command to run the scroller script after you "cd" to the directory where the scroller file resides:

    Code:
    ksh scroller
    Here's a way you can make it even more convenient:

    1. Give the scroller file execute permission:
    Code:
    chmod +x scroller
    2. Edit the scroller script (if you know how to use a Terminal-based text editor). Set the first line to:
    Code:
    #! /bin/ksh
    Then you can invoke scroller in any shell (and it will run under the Korn shell).

    For example, you could type
    Code:
    ./scroller
    or
    Code:
    /some/path/scroller
     
  7. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #7
    #! /usr/bin/ksh
    #
    # scroller --- display text, but sleep every few lines.
    #
    #
    # scroller will copy 5 lines from standard input to the
    # local terminal and then sleep for one second.
    #
    # You can override the 5 lines with a "-l n" option.
    # And you use a "-s n" to override the sleep time.
    #
    # You can type your interrupt character (often control c)
    # to get a menu that will let you adjust these values.
    #
    # You can use -m to go straight to the menu at startup
    #
    # You can specify filenames or scroller can just read from
    # standard in.
    #
    USAGE="usage: scroller [-s n] [-l n] [-m] [filename1] [filename2] ..."

    #
    # initialize variables
    #
    # Set reasonable initial values here. TASK controls the main
    # loop as described below. snooze is the number of seconds for
    # the sleep. lpp (lines per page) is the number of lines to
    # dispaly before sleeping. lines will repeatedly cycle from 0
    # to npp as the program runs. totallines is a count of lines
    # processed so far. And we turn off IFS so the reads work with
    # data that has leading white space.

    TASK=2
    snooze=1
    lpp=5
    lines=0
    totallines=0
    IFS=""

    #
    # Now Process the command line
    #

    error=0
    while getopts :s:l:m o ; do
    case $o in
    s) if [[ $OPTARG -gt 0 && $OPTARG -lt 100 ]] ; then
    snooze=$OPTARG
    else
    print $OPTARG is illegal
    error=1
    fi
    ;;
    l) if [[ $OPTARG -gt 0 && $OPTARG -lt 100 ]] ; then
    lpp=$OPTARG
    else
    print $OPTARG is illegal
    error=1
    fi
    ;;
    m) TASK=2
    ;;
    ?) print error argument $OPTARG is illegal
    error=1
    ;;
    esac
    done
    shift OPTIND-1
    if ((error)) ; then
    print $USAGE
    TASK=0
    fi


    #
    # Open first file or indicate that we are using stdin
    #

    if (($# >= 1)) ; then
    input=$1
    exec < $input
    shift
    else
    input="(standard input)"
    fi


    #
    # Major loop. TASK can be 0 or 1 or 2. If TASK is set we
    # loop doing either task 1 or task 2. Either task can switch
    # to the other. And both tasks can abort by setting TASK=0.
    # But control c will switch to task 1.
    #

    trap interrupt=1 2
    interrupt=0
    while ((TASK)) ; do
    if ((interrupt)) ; then
    interrupt=0
    TASK=1
    fi
    case $TASK in

    #
    # Task 1 --- display service menu
    #

    1) print
    print '=====[[ scroller service menu ]]====='
    print
    print " $totallines lines processed so far from $input"
    print " sleep seconds = $snooze"
    print " lines per page = $lpp"
    print
    print ' 1) list the text'
    print ' 2) set lines per page'
    print ' 3) set sleep seconds'
    print ' 4) abort'
    print
    print -n "[Enter Selection]====>>"
    read < /dev/tty
    case $REPLY in
    1) TASK=2
    ;;
    2) print lines per page is currently $lpp
    print -n enter new value --
    read val < /dev/tty
    if [[ $val -gt 0 && $val -lt 100 ]] ; then
    lpp=$val
    else
    print $val is illegal
    fi
    ;;
    3) print sleep seconds is currently $snooze
    print -n enter new value --
    read val < /dev/tty
    if [[ $val -gt 0 && $val -lt 100 ]] ; then
    snooze=$val
    else
    print $val is illegal
    fi
    ;;
    4) TASK=0
    ;;
    *) print illegal response
    print REPLY = $REPLY
    ;;
    esac
    ;;

    #
    # Task 2 --- copy lines from stdin to stdout
    #
    # Read a line, increment counts, sleep if it's time
    #

    2) if read line ; then
    print -r -- "$line"
    ((totallines=totallines+1))
    ((lines=lines+1))
    if ((lines >= lpp)) ; then
    lines=0
    sleep $snooze
    fi
    else
    #
    # No more input. Open a new file or give up.
    #

    if [[ $# -ge 1 ]] ; then
    input=$1
    exec < $input
    shift
    else
    TASK=0
    fi
    fi
    ;;

    #
    # Task * --- can't happen
    #
    *) print -u2 $0: impossible error TASK = $TASK
    TASK=0
    ;;
    esac
    done

    ((totallines)) && print "$totallines" total lines processed

    exit 0
     
  8. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #8
    Got the above script, from a unix website. I don't know how it is written out. I copy and pasted it from my iPhone, I don't know if it is displayed correctly or not, I also forgot how to post code to this site. Maybe it would display differently on a computer? You can google "is there a way to scroll text" on google to find the code it is on a unix and Linux site, I think it is the first hit. My question is how do I use scroller with commands from the command line? I was able to use scroller with files but I want to scroll like with the ls command. I tried just typing scroller without a filename, the cursor went to the next line, but when I typed ls and return my computer just repeated ls. Like my computer just repeated what I had typed. Like ls was written twice and the cursor went to the next line. I don't know if the program wanted a filename? Within the script it says you can specify a filename or scroller will read from standard in, what does that mean exactly? Can I use scroller with commands like ls? Sorry about the code I posted, I am using my iPhone and when I copied the scroller script, I can't scroll up in the post reply window. Any help would be appreciated.
     
  9. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #9
    It looks like you use it as follows with other commands:
    Code:
    ls | scroller
    That's a vertical bar character, which is called a "pipe" symbol. You are "piping" the output of the ls command to the scroller command, which should make the output appear slowly.

    There is another command (which comes with the Terminal environment) that does something similar. If you type
    Code:
    ls | more
    then the output will appear one screenful at a time. Press the space bar to go to the next screenful. Just like scroller, you can use the more command with files, e.g.
    Code:
    more myfile.txt
    By the way, if you want to display code in a post, you can put
    Code:
     tags around it.
     

Share This Page