LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-15-2004, 04:53 PM   #1
statmobile
Member
 
Registered: Aug 2003
Location: Chapel Hill, NC
Distribution: Gentoo, Windows 95 2000 & XP
Posts: 160

Rep: Reputation: 30
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.
 
Old 07-15-2004, 05:52 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 07-15-2004, 09:36 PM   #3
statmobile
Member
 
Registered: Aug 2003
Location: Chapel Hill, NC
Distribution: Gentoo, Windows 95 2000 & XP
Posts: 160

Original Poster
Rep: Reputation: 30
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
 
Old 07-16-2004, 07:35 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.
 
Old 07-16-2004, 09:20 PM   #5
statmobile
Member
 
Registered: Aug 2003
Location: Chapel Hill, NC
Distribution: Gentoo, Windows 95 2000 & XP
Posts: 160

Original Poster
Rep: Reputation: 30
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
 
Old 07-16-2004, 10:16 PM   #6
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.
 
Old 07-16-2004, 11:35 PM   #7
statmobile
Member
 
Registered: Aug 2003
Location: Chapel Hill, NC
Distribution: Gentoo, Windows 95 2000 & XP
Posts: 160

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
initialization files run multiple times Brad Richards Linux - Newbie 2 02-19-2005 10:56 AM
Bash backup script - If multiple files starting with a exist problem demoncheese Programming 2 07-29-2004 10:47 PM
Script to run script in multiple directories... seabass55 Linux - General 5 07-06-2004 12:44 AM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 10:45 AM


All times are GMT -5. The time now is 11:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration