Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    1

    Red face Unanswered: Help with command line arguments!

    Here's what I have so far. Yes, yes, I know. CSH. I MUST use it however.

    code:--------------------------------------------------------------------------------

    #!/bin/csh -f

    set params = $argv[1-]
    set argc = $#argv
    set lsval = `ls`
    set title = "File Index"

    #defines head
    echo "<html>"
    echo "<head>"
    echo "<title>"
    echo "$title"
    echo "</title>"
    echo "</head>"
    echo "<body>"
    echo "<h1>"
    echo "$title"
    echo "</h1>"
    echo "<hr>"
    echo "<table>"

    #begin loop
    set var = `ls`
    foreach a($var)


    echo "<tr>"
    echo "<td>"
    echo "<pre>"

    echo " $a "

    echo "</pre>"

    echo "</td>"

    echo "</tr>"

    end
    #loop end

    #defines bodyend
    echo "</table>"
    echo "<hr>"
    echo "<address>"
    echo "Generated by $USER@`hostname` from $HOME"
    echo "</address>"

    --------------------------------------------------------------------------------


    The problem I have now, is that I don't know how to 'scan' the argument list stored in argv. Ideally, I want the program to be run

    uprompt> ./htx2 *.html > index.html

    So this means whatever the extension is , say *.c *.h, the script will find it in argv, and place it in var, so var = `ls *.html` say. Then only files with the html extension will be listed in the html file.

    How can I do this. I've tried and cannot seem to get it. The users input, *.html, or *.c or whatever (including multiple wildcards), can occur anywhere in the argv line. So it could follow other switches, whatever. How can I scan the line, and pass the search term to ls?
    Last edited by denizengt; 10-29-03 at 09:48.

  2. #2
    Join Date
    Oct 2003
    Posts
    18
    But you don't need to parse the command line and expand wildcards yourself. The shell does it for you. If users run your script with

    ./htx2 *.html > index.html

    then your $argv will contain a list of all the html files, not the string "*.html". Was this what you were looking for? Maybe I misunderstood your question.

    And why on earth do you need to use csh?

    /Hacker

  3. #3
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697

    Re: Help with command line arguments!

    How does this work for ya:

    set title = "File Index"

    #defines head
    cat - <<HEADER
    <html>
    <head>
    <title>
    $title
    </title>
    </head>
    <body>
    <h1>
    $title
    </h1>
    <hr>
    <table>
    HEADER

    #begin loop
    foreach a($*)

    The rest is straightforward.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •