LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   run script on multiple files (https://www.linuxquestions.org/questions/programming-9/run-script-on-multiple-files-205523/)

statmobile 07-15-2004 04:53 PM

run script on multiple files
 
Hello all, I have a script I'm trying to run on multiple files. I believe the problem is that it runs as ./script filename.html.

It uses the filename in the script as $1, and I think this may be the problem with why the script won't work for
./script *.html

Here is the abridged version.
Code:

#!/bin/sh
grep '<a href="/link.html">Data Analysis</a> |' $1 && X=1 || X=0
if test $X = "1" ; then
        echo $PWD"/"$1 "has the Data Analysis link"
        mv $1 $1.old.html
else
        echo $PWD"/"$1 "does not have the Data Analysis link"
fi

Any help would be greatly appreciated. Scripting seems so easy if you're a fan of CLI like me, but yet I keep having trouble with the oddest things.

jschiwal 07-15-2004 05:52 PM

You could use the shift command to shift the arguments down one each time. You could use a for loop with the $* pattern to iterate through each argument. Or you could use the pattern '*.html' inside the script in a for loop
E.g. for file in *.html; do <your code>; done.

Another thing you might consider is using a SED or AWK script to search for the pattern in the file.

statmobile 07-15-2004 09:36 PM

Is that the problem, because doing
./script *.html
is the same as writing
./scipt file1.html file2.html file3.html etc.
as opposed to
./script file1.html
./script file2.html
./script file3.html

osvaldomarques 07-16-2004 07:35 PM

Hi statmobile,
One of your problems is the shell interprets the '*' before the execution of your script. So, it passes a list of parameters for you to deal with. If you want to receive the '*' you need to pass the parameter surrounded by apostrophes: ./script ´*.html' or ./script \*.html so you can treat it for yourself.
But, for general purpose, all you need is loop you script with the shift command as recommended by jschiwal.
So, your script could edited to be like this
Code:

#!/bin/sh
while :
do
  if [ "$#" = "0" ]
  then
    exit
  fi
  grep '<a href="/link.html">Data Analysis</a> |' $1 && X=1 || X=0
  if test $X = "1" ; then
        echo $PWD"/"$1 "has the Data Analysis link"
        mv $1 $1.old.html
  else
        echo $PWD"/"$1 "does not have the Data Analysis link"
  fi
  shift
done

The shift command discards the first parameter and shifts all the remaining until there is no more. The "$#" tells us the amount of parameters the script has.

statmobile 07-16-2004 09:20 PM

thanks, I ended up doing the same basic thing. The shift command was key. I couldn't run the *.html loop in the script, because I wanted to run it recursivel through other directories, and that just seemed too complicated.

Thanks

osvaldomarques 07-16-2004 10:16 PM

In this case, you have to pass the directory as a parameter and optionally, the single quoted file name mask as the second parameter to the shell.
On the start you do some checks:
if [ "$#" = "0" ]; then
DIR=`pwd`
else
DIR=$1
fi
if [ "$#" = "2" ]; then
FLIST=$2
else
FLIST='*.html'
fi
For this, you have to pass the parameter single quoted and use find to discover your files. In the line I put "while :", you replace with
Code:

for ARQ in `find "$DIR" -type f -name "$FLIST"`
In your grep line, you must replace the "$1" parameter by "$ARQ".
Remove the shift.
Done.
Find will look for files with you specification and give it one by one for the loop. It will be recursive, so all sub-directories will be searched. If you don't give any parameter, it will look for "*.html" startin in the current directory. If you give only the directory, it will look for "*.html" starting in that directory. If you give a directory and '*.php" it will look for php files instead of html ones.

statmobile 07-16-2004 11:35 PM

thanks osvaldomarques, but I'm all set with this post for now. I have accomplished the task at hand, and quite well due to both of your help. I am extremely appreciative.


All times are GMT -5. The time now is 04:19 PM.