Use wildcards in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use wildcards in a script
# 1  
Old 05-11-2007
Use wildcards in a script

Hello

I have this script:

#!/bin/ksh

INPUTFILE=$1
TEMPFILE=$INPUTFILE.$$
OUTPUTFILE=$INPUTFILE.new

# nr of arguments has to be 1
if [ $# -ne 1 ]
then
echo "\nUsage: $0 inputfile\n"
return 1
fi

# inputfile must exist and be readable
if [ ! -r $1 ]
then
echo "\nInputfile $1 does not exist. Please enter a valid filename\n"
return 1
fi

echo "\nConvert File $INPUTFILE to $OUTPUTFILE"
tr "ÖÓÒÕÔØöóòõôÄÁÀÃÂäáàãâåÜÚÙÛüúùûËÉÈÊëéèêÏÍÌÎïíìîÑñÇçªß¿"
"OOOOOOoooooAAAAAaaaaaaUUUUuuuuEEEEeeeeIIIIiiiiNnCcaS?" < $INPUTFILE >
$OUTPUTFILE

# Change ß to SS
# sed s/ß/SS/g $OUTPUTFILE > $TEMPFILE
# mv $TEMPFILE $OUTPUTFILE

echo "...ready\n"

Which does conversion in file names to the US standard. It works for
just one file at a time. I want to be able to do as the INPUTFILE a
wildcard, for example: File_*.txt.

Can someone help me on that?

Regards,

Eduardo Ferrari
# 2  
Old 05-11-2007
Eduardo,
See if this would work for you:
(Note: Since you are already looping thru files, there is no need to test them
for their existence nor for the number of parameters)
Code:
#!/bin/ksh
for INPUTFILE in File_*.txt
do
  TEMPFILE=$INPUTFILE.$$
  OUTPUTFILE=$INPUTFILE.new

  echo "\nConvert File $INPUTFILE to $OUTPUTFILE"
  tr "VSRUTXvsrutDA@CBda`cbe\ZY[|zy{KIHJkihjOMLNomlnQqGg*_?"
  "OOOOOOoooooAAAAAaaaaaaUUUUuuuuEEEEeeeeIIIIiiiiNnCcaS?" < $INPUTFILE > $OUTPUTFILE

  # Change _ to SS
  # sed s/_/SS/g $OUTPUTFILE > $TEMPFILE
  # mv $TEMPFILE $OUTPUTFILE

  echo "...ready\n"
done

# 3  
Old 05-11-2007
Quote:
Originally Posted by emferrari
Hello

I have this script:

Please put code inside code tags.

Quote:
#!/bin/ksh

INPUTFILE=$1
TEMPFILE=$INPUTFILE.$$
OUTPUTFILE=$INPUTFILE.new

# nr of arguments has to be 1
if [ $# -ne 1 ]
then
echo "\nUsage: $0 inputfile\n"
return 1
fi

# inputfile must exist and be readable
if [ ! -r $1 ]
then
echo "\nInputfile $1 does not exist. Please enter a valid filename\n"
return 1
fi

echo "\nConvert File $INPUTFILE to $OUTPUTFILE"
tr "ÖÓÒÕÔØöóòõôÄÁÀÃÂäáàãâåÜÚÙÛüúùûËÉÈÊëéèêÏÍÌÎïíìîÑñÇçªß¿"
"OOOOOOoooooAAAAAaaaaaaUUUUuuuuEEEEeeeeIIIIiiiiNnCcaS?" < $INPUTFILE >
$OUTPUTFILE

# Change ß to SS
# sed s/ß/SS/g $OUTPUTFILE > $TEMPFILE
# mv $TEMPFILE $OUTPUTFILE

echo "...ready\n"

Which does conversion in file names to the US standard. It works for
just one file at a time. I want to be able to do as the INPUTFILE a
wildcard, for example: File_*.txt.

Put your script in a loop:

Code:
for file in "$@"
do
   : put your code here; use "$file" for the filename
done

Call the script with:

Code:
name_of_script File_*.txt

# 4  
Old 05-12-2007
Thanks all, I will test Monday and let everybody knows!

Eduardo Ferrari
# 5  
Old 05-14-2007
Hi

So, now my script is like this:

for file in "$@"
do

INPUTFILE=$file
TEMPFILE=$INPUTFILE.$$
OUTPUTFILE=$INPUTFILE.new

# nr of arguments has to be 1
if [ $# -ne 1 ]
then
echo "\nUsage: $0 inputfile\n"
return 1
fi

# inputfile must exist and be readable
if [ ! -r $1 ]
then
echo "\nInputfile $file does not exist. Please enter a valid filename\n"
return 1
fi

echo "\nConvert File $INPUTFILE to $OUTPUTFILE"
tr "ÃÃÃöôÃäãÃÃüÃëêÃïîê¿"
"OOOOOOoooooAAAAAaaaaaaUUUUuuuuEEEEeeeeIIIIiiiiNnCcaS?" < $INPUTFILE >
$OUTPUTFILE

# Change à to SS
# sed s/Ã/SS/g $OUTPUTFILE > $TEMPFILE
# mv $TEMPFILE $OUTPUTFILE

echo "...ready\n"

done


[mantas@IDBBANKMANTASDEV test]$ ./convertspecch.sh File*.txt
./convertspecch.sh[33]: syntax error: `newline' unexpected

What's wrong?
# 6  
Old 05-14-2007
Please put code inside code tags.

Your loop is misplaced.
When you split a command over several lines, don't forget to put the continuation character \ as the last character of lines to be continued.

Code:
# nr of arguments has to be at least 1
if [ $# -lt 1 ]
then
   echo "\nUsage: $0 inputfile(s)\n"
   return 1
fi

for inputfile in "$@"
do

   # inputfile must exist and be readable
   if [ ! -r $inputfile ]
   then
      echo "\nInputfile $inputfile does not exist. Please enter a valid filename\n"
      return 1
   fi

   outfile=$inputfile.new

   echo "\nConvert File $inputfile to $outputfile"
   tr "ÃÃÃöôÃäãÃÃüÃëêÃïîê¿" \
      "OOOOOOoooooAAAAAaaaaaaUUUUuuuuEEEEeeeeIIIIiiiiNnCcaS?" \
      < $inputfile >$outputfile

    echo "...ready\n"

done

Jean-Pierre.
# 7  
Old 05-14-2007
Quote:
Originally Posted by emferrari
Hi

So, now my script is like this:

I repeat: Please put code inside CODE tags.

The following code doesn't give me any syntax errors, but I fixed the line breaks. Could that be your problem?

There are other issues with the script.
Quote:
for file in "$@"
do

INPUTFILE=$file
TEMPFILE=$INPUTFILE.$$
OUTPUTFILE=$INPUTFILE.new

# nr of arguments has to be 1
if [ $# -ne 1 ]
then
echo "\nUsage: $0 inputfile\n"
return 1
fi

You don't need to check for number of arguments, and if you give more than one file on the command line, this will cause the script to exit.

To exit from a script, use exit, not return; return is used to exit from functions and sourced scripts.
Quote:
# inputfile must exist and be readable
if [ ! -r $1 ]
then
echo "\nInputfile $file does not exist. Please enter a valid filename\n"
return 1
fi

echo "\nConvert File $INPUTFILE to $OUTPUTFILE"
tr "ÃÃÃöôÃäãÃÃüÃëêÃïîê¿"
"OOOOOOoooooAAAAAaaaaaaUUUUuuuuEEEEeeeeIIIIiiiiNnCcaS?" < $INPUTFILE >
$OUTPUTFILE

There should be the same number of characters in both strings (or more in the first).

Quote your variables. That will fail if any filenames contain spaces.

Quote:
# Change à to SS
# sed s/Ã/SS/g $OUTPUTFILE > $TEMPFILE
# mv $TEMPFILE $OUTPUTFILE

echo "...ready\n"

done


[mantas@IDBBANKMANTASDEV test]$ ./convertspecch.sh File*.txt
./convertspecch.sh[33]: syntax error: `newline' unexpected

What's wrong?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wildcards in file input to a script?

I have four files: test test2 test3 test4 I have this simple script: #!/bin/bash ls $1 Why does ./the_script.sh test* only list the first file, when a normal ls test* would list all four? What do I need to change in the script to be able to use wildcard? (12 Replies)
Discussion started by: KidCactus
12 Replies

2. Shell Programming and Scripting

SH Script for file name wildcards

Does anyone know how I would go about inserting text at the beginning of a file with the file name containing a daily time stamp? Essentially I need to find the file name using a wild card, and then insert 3 lines of text - one of which is the processing date. Help please!? (1 Reply)
Discussion started by: cookie33
1 Replies

3. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

4. UNIX for Dummies Questions & Answers

Running script on SFTP server, RMDIR and RM with wildcards

Im going insane trying to figure out what i consider a basic command on an SFTP server... Im trying to download all files from a directory *done* then remove all the files (and sometimes folders that contain files) i have downloaded on the remote directory... the command i would normally... (2 Replies)
Discussion started by: mokachoka
2 Replies

5. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies

6. UNIX for Dummies Questions & Answers

script for deletion using wildcards

find ./ -name t\* | sed "s@^./@@" > .filestobedeleted j=$(wc -l < .filestobedeleted) typeset -i cnt=0 typeset -i i=0 while read line do myarray=$line ((cnt = cnt + 1)) done < .filestobedeleted while (1 Reply)
Discussion started by: aishu
1 Replies

7. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

8. UNIX for Dummies Questions & Answers

ls with wildcards

ok, I'm trying to write a script file that lists files with specific elements in the name into a txt file, it looks like this ls s*.dat > file_names.txt can't figure out whats wrong with that line, any ideas? thanks in advance (10 Replies)
Discussion started by: benu302000
10 Replies

9. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question