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

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)

#!/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

Please put code inside code tags.

Put your script in a loop:

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

Call the script with:

name_of_script File_*.txt

Thanks all, I will test Monday and let everybody knows!

Eduardo Ferrari

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?

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.

# 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.

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.

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.

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.

hi, i have a similar question,

i would like to view multiple files using cat, but i don't know the correct command to use.

example:
i have the following files
file12345
file12346
file12347
file12348
and so on up to....
file43455

is there a one-line command that can cat all these files?

i know about wildcards, but what i'm thinking is to issue the following commands:

cat file1234[5-9]
cat file123[4-9]?
cat file12[4-9]??

and so on.. but these are a set of commands that does the cat'ing part by part... can i cat all those files using a one-line command??

thanks for helping

Depending on what you want:

cat file*

Or:

cat file[0-9][0-9][0-9][0-9]

To see which files will be matched by a pattern without running any command on them, use:

printf "%s\n" PATTERN
## e.g.: printf "%s\n" file*

yes, im aware of that, but my concern is that i would like to have a one-line command that lists files file12345-file34224. I know that I can't just issue the command

cat file[1-3][2-4][3-2][4-5]

because it will not include all other files i want. i have a set of files with sequence numbers but i just want to display a portion of it using a one line command.

if there is an alternative way to do this?

to make it short, i have a set of files from file00000 up to file99999. I just want to display files from file12345-file34224. if anyone can give a one line command for it, i can take it from that example.

What files do you want?

The patterns I posted will give you all the files
a) that begin with "file" and
b) all the files that are the word "file" followed by 4 digits.

What more do you want?

i have a set of files from file00000 up to file99999. I want to display files from file12345-file34224.

can you please provide a one line command for it, so that i can take the logic from your answer?

Well, there is nothing wrong with a loop on the commandline:

filenr=12345;while [ $filenr -le 34224 ] ; do cat file$filenr ; (( filenr += 1 )) ; done

You could of course also write a little script:

#! /bin/ksh

typeset -i start=$1
typeset -i end=$2
typeset -i counter=$start

while [ $counter -le end ] ; do
     if [ -r file$counter ] ; do
          cat file$counter
     fi
     (( counter += 1 ))
done

exit 0

Save this as "auto-cat.sh", give it execute rights and use it like "auto-cat.sh 1 5" to display file1, file2, ...., file5

bakunin

thanks bakunin and cfajohnson!

i wasn't aware that we can issue a set of looping commands using delimiter ;

great help!