#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0

    How to return argument of command line?


    I want to change the href path in many files. So, i write a simple script and save it into a file to run it.

    for i in filename1, filename2
    do
    sed 's/src=/src=..\/page\//' $i > change.tmp
    cp -f change.tmp $i
    done

    so, in order to change some specific file, i hv to go into the file and change the filename manually.. I wish to make it more effective that it can return the argument of unix commandl line and run the program. so, i no need to hardcored the filename in the script.

    eg. change a.html
    # change is the script name, so, a.html will pass into the change file and run the script inside change the path of a.html.

    anyone can give me some idea how to do it? Druuna, i hope u can see this message and teach me how to do..

    thanks...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    I'm not certain that I understand the question.

    This is what I think you want:

    1) change href="whatever" to href="your-choice" in a specific file.
    2) give the name of the file(s) to change as option to a script.

    Another question:

    What do the original scr="...." look like, or are they empty. I need to know this because of the regular expression that needs to be constructed for the sed statement.
    Last edited by druuna; November 21st, 2003 at 11:52 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0
    drunna, actually wat u understand is correct.

    here is the situation, there are some files tat i need to change the file path for each files. So, i dun wan manually change it one by one. So, i wan to write some script and included all those file in tat script in order to run the process.

    for the src question, ../page is go out for the current directory and get into another page directory! I test it, it is work properly.. so, no need to bother abt this question.

    my question is there any shell script tat can retrieve or return the argument of the unix command?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    The command line arguments are passed to shell scripts via $1, $2, $3, ...; "$*" contains all of them.


    #!/bin/sh
    for i in $*; do ...

    Second solution:
    put them in a text file, say /tmp/files.txt
    then use:
    for i in `cat /tmp/files.txt`; do ...

    Another one:
    pipe the file to the script and make the script read from stdin:
    cat /tmp/files.txt | script.sh
    (or: script.sh < /tmp/files.txt)

    while read filename; do...

    hth,
    M.
    Last edited by M.Hirsch; November 27th, 2003 at 01:56 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0
    Thanks for the so many alternative solution..

    I try all of those solution. The first one work perfectly.. But the second and third solution is not working.

    For the second solution, i put the file.txt and the script.sh into same directory. some error msg appear like this : "cannot find and open file cat"
    for i in 'cat file.txt'
    do
    sed 's/src=/src=..\/page\//' $i > change.tmp
    cp -f change.tmp $i
    done

    for the third solution, it appear msg like "file.txt : this is not an identifier"
    while read file.txt
    do
    sed 's/src=/src=..\/page\//' $i > change.tmp
    cp -f change.tmp $i
    done

    May i noe y this kinds of errors appear? Thanks~
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    #2 failed because this is supposed to be backticks, not single quotes. ` not '
    But on second thought, don't use it. I guess it will fail when filenames contain spaces (untested).

    #3 was a little misleading, sorry, I should have said some more words.
    "filename" was the variable name here.
    So:
    script.sh:
    Code:
    #!/bin/sh
    while read i
    do
    sed 's/src=/src=..\/page\//' "$i" > change.tmp
    cp -f change.tmp "$i"
    done
    And then: ./script.sh < ./files.txt
    Also add the quotes around "$i" to deal with spaces in filenames.

    hth,
    M.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0
    Thanks M.Hirsch~ I test it again with ur instruction. All is work perfectly~
    Thanks to drunna also, u help me a lots too~

    I am very glad found this forum with so many helpful frens here~ Have a nice day, guys!
    Thanks a lots!!!

IMN logo majestic logo threadwatch logo seochat tools logo