The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script for deletion using wildcards aishu UNIX for Dummies Questions & Answers 1 01-09-2008 05:37 PM
ls with wildcards benu302000 UNIX for Dummies Questions & Answers 10 06-29-2005 01:53 PM
wildcards benu302000 UNIX for Dummies Questions & Answers 3 06-29-2005 12:10 PM
Wildcards in VI peter.herlihy UNIX for Dummies Questions & Answers 8 01-08-2002 04:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-11-2007
emferrari emferrari is offline
Registered User
  
 

Join Date: May 2007
Location: New York, NY
Posts: 3
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 (permalink)  
Old 05-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 05-11-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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 (permalink)  
Old 05-12-2007
emferrari emferrari is offline
Registered User
  
 

Join Date: May 2007
Location: New York, NY
Posts: 3
Thanks all, I will test Monday and let everybody knows!

Eduardo Ferrari
  #5 (permalink)  
Old 05-14-2007
emferrari emferrari is offline
Registered User
  
 

Join Date: May 2007
Location: New York, NY
Posts: 3
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 (permalink)  
Old 05-14-2007
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 (permalink)  
Old 05-14-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0