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 |
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. |
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 |
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 |
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 |
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"` 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. |
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. |