Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Posts
    2

    Unanswered: shell scripting help

    Guys need some help on this.

    (a)
    I got 40 scripting files (.sh) in a directory.
    Each file has a line called
    ....
    javac X.java
    ....
    I need to replace X with the name of the file, so if file name is abc1.sh and abc2.sh then in both these files the line would read as
    ....
    javac abc1.java //# or javac abc2.java as for other file.
    ....

    Can i write a script which will traverse all these files one by one and replace the string "X" with the name of the file?

    (b)
    I got 40 scripting files (.sh) in a directory, like abc1.sh, abc2.sh, abc3.sh.. and so on till abc40.sh. I need to create as many java files corresponding to each, ie, abc1.java, abc2.java .... abc40.java. I dont care about contents in them.

    Can i acheive it throu a script?

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Looks like homework
    Code:
    for fn in abc*.sh
    do
      fnx=`basename $fn ".sh"`
      sed 's/X.java/'$fnx'.java/' $fn >$fnx.sh.new
      mv $fnx.sh.new $fn
      touch $fnx.java
    done

  3. #3
    Join Date
    Sep 2005
    Posts
    2

    thanks

    Thanks, even though i find it very difficult to undertand what is line 2 doing these lines do solve my problem.

    Would it be possible to explain what do you do in line 2?

    sed 's/X.java/'$fnx'.java/' $fn >$fnx.sh.new

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Using sed to edit the file and output is redirected to a new file

    Code:
    man sed
    
    ---snip, snip---
    The sed utility is a stream editor that reads one or more
    text files, makes editing changes according to a script of
    editing commands, and writes the results to standard output.
    ---snip, snip---
    s/regular expression/replacement/flags
    
    Substitute  the  replacement  string  for
    instances of the regular expression in the
    pattern space.  Any character other  than
    backslash or newline can be used instead of a
    slash to delimit the RE and the replacement.

Posting Permissions

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