Shell script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrw133
    New Member
    • Jan 2008
    • 5

    Shell script

    So im working on a lab for my unix class and im having some problems with this shell script we are supposed to do here is the question: Write a shell script that accepts a list of files(space delimited), and does the following 1)lists a long listing of the file
    2) displays the number of lines in the file
    3) displays the first 2 lines of the file
    4) after listing all files, displays a friendly goodbye message.
    separate the files with some dashes and make it easy to read.
    heres my script:

    #!/bin/bash

    while [ "$1" != "" ]
    do

    ls -la$1
    shift

    wc
    shift

    head-n2
    shift

    echo goodbye
    done

    exit 3

    now he gave an example in class thats like this:

    #!/bin/bash

    # This is a Comment Line
    while [ "$1" != "" ]
    do
    ls -la $1
    shift
    done

    exit 0

    he said that this shell script is basically this but i had to add in the other commands besides the long listing. any help will be great. thanks
  • WinblowsME
    New Member
    • Jan 2008
    • 58

    #2
    Originally posted by jrw133
    heres my script:

    #!/bin/bash

    while [ "$1" != "" ]
    do

    ls -la$1
    shift

    wc
    shift

    head-n2
    shift

    echo goodbye
    done
    You're on the right track. Everytime you use shift, your command-line arguments are shifted to the left. So, if the name of your script is test_script and you run the script with the following arguments test_script A B C D, $1 will be A, and if you use shift, A will be shifted out and $1 will become B, and so forth... Also, you might want to check out the man page for the wc command. I'm pretty sure you need to use an option to list the line count.

    Comment

    Working...