Unix/Linux Go Back    


Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

Get file name in shell scrip loop: bad substitution

Solaris


Tags
looping, shell script, substring

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-02-2011   -   Original Discussion by soueric
soueric's Unix or Linux Image
soueric soueric is offline
Registered User
 
Join Date: May 2011
Last Activity: 7 December 2017, 5:53 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Get file name in shell scrip loop: bad substitution

Hi guys. Good day, morning, afternoon or night, depending on where you live.
I have a script shell in which I am looping on files (absolute path) see code section above.
I always have an error: bad substitution.
Linux
Is it because my variable file is the index of the loop and not a normal string?

Code:
for file in $home/$input/*.txt
do
    echo ${file##*/}.log
done

I specify that I need to loop on the absolute path, as I do not want to go inside the files folder, as I am running the script from a bin folder and want to stay in there.

Thanks for your help.
Sponsored Links
    #2  
Old Unix and Linux 05-02-2011   -   Original Discussion by soueric
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 November 2017, 9:59 PM EST
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts
Is that you looking for ?


Code:
for file in $home/$input/*.txt do     basename $file | sed 's/txt/log/'  done

Sponsored Links
    #3  
Old Unix and Linux 05-02-2011   -   Original Discussion by soueric
soueric's Unix or Linux Image
soueric soueric is offline
Registered User
 
Join Date: May 2011
Last Activity: 7 December 2017, 5:53 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi itkamaraj.
Thanks, it's working well.
I have actually customized it into:


Code:
for file in $home/$input/*.txt do     basename $file | sed 's/.txt//'  done

Actually, I will reuse this base name for generating other logs file containing different extensions (bad.log, discard.log...)

Issue is solved.
Thanks.
    #4  
Old Unix and Linux 05-02-2011   -   Original Discussion by soueric
rdcwayx's Unix or Linux Image
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 13 September 2015, 8:58 PM EDT
Posts: 2,759
Thanks: 44
Thanked 420 Times in 408 Posts

Code:
for file in $home/$input/*.txt
do 
  echo ${file%%.*}.log
done

Sponsored Links
    #5  
Old Unix and Linux 05-03-2011   -   Original Discussion by soueric
soueric's Unix or Linux Image
soueric soueric is offline
Registered User
 
Join Date: May 2011
Last Activity: 7 December 2017, 5:53 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi rdcwayx.
Thanks for you answer, my problem was solved.

However, your command does not work as it is still returning the entire path I wanted to get rid:

Code:
/........./filename.log

So first solution suggested was better.
Thanks.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to combine two files into one file using shell scrip netbanker Shell Programming and Scripting 2 12-06-2010 06:34 PM
How to convert shell scrip to binaric command raghunsi UNIX for Advanced & Expert Users 1 02-03-2010 02:27 AM
Using cp command inside shell scrip yoavbe Shell Programming and Scripting 2 06-22-2006 10:57 AM
If a is windows gui ( client), b is a unix gui ( Server for a) and c is a shell scrip hchivukula UNIX for Dummies Questions & Answers 1 06-12-2005 09:34 AM
Execute an Oracle stored procedure from a shell scrip mh53j_fe Shell Programming and Scripting 1 06-03-2005 04:17 PM



All times are GMT -4. The time now is 08:55 AM.