Accepting filename as command line param and writing to it | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Accepting filename as command line param and writing to it

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-06-2008
silas.john silas.john is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 30 September 2008, 11:31 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Accepting filename as command line param and writing to it

Hi,

Is it possible to accept a filename as command line parameter and then write to that file using command redirection? i tried the below script.

outputfile=`echo $1`

echo "Writing to file" > 'echo $outputfile'

exit $returncode


but it isnt working. is there any other way to write to a file whose filename is accepted as a parameter?
Sponsored Links
    #2  
Old 02-06-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 November 2014, 9:00 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
Try this:


Code:
outputfile=$1
echo $outputfile
echo "Writing to file" > $outputfile

Read some scripting tutorials or books.

Regards
Sponsored Links
    #3  
Old 02-06-2008
manas_ranjan's Avatar
manas_ranjan manas_ranjan is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 17 November 2014, 5:53 AM EST
Location: Amsterdam
Posts: 273
Thanks: 12
Thanked 0 Times in 0 Posts
try this

i would suggest trying to solve a problem is good.....but after then rectification is also required....so can u please look below my suggestions and try to check why u r solution is having problem...??? can u do it...


File_Name=$1
Path="/xx/yy"

echo " Welcome....." >> $Path/$File_Name
    #4  
Old 02-06-2008
silas.john silas.john is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 30 September 2008, 11:31 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Franklin52 View Post
Try this:


Code:
outputfile=$1
echo $outputfile
echo "Writing to file" > $outputfile

Read some scripting tutorials or books.

Regards
Nope. this doesnt work. infact i had tried this one too
Sponsored Links
    #5  
Old 02-06-2008
silas.john silas.john is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 30 September 2008, 11:31 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by manas_ranjan View Post
i would suggest trying to solve a problem is good.....but after then rectification is also required....so can u please look below my suggestions and try to check why u r solution is having problem...??? can u do it...


File_Name=$1
Path="/xx/yy"

echo " Welcome....." >> $Path/$File_Name
i guess ur code doesn work either..says ksh [programfilename] cannot execute
Sponsored Links
    #6  
Old 02-06-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 November 2014, 9:00 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
Show your entire script. Have you make your script executable with chmod?

Regards
Sponsored Links
    #7  
Old 02-06-2008
silas.john silas.john is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 30 September 2008, 11:31 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
HI...its working..

but i dont understand what could be difference between

outputfile=`echo $1`

and

outputfile=$1

This is the only difference in our code.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pass a filename as a command line argument ektubbe Shell Programming and Scripting 1 05-01-2012 04:25 AM
Accepting command line arguments in bash kristinu UNIX for Dummies Questions & Answers 17 03-16-2012 01:21 PM
Preserving newlines when writing loops on the command line in bash JohnK1 Shell Programming and Scripting 2 03-02-2011 04:42 PM
grep not accepting variable filename bdrelling Shell Programming and Scripting 1 02-20-2011 11:12 PM
Change filename extensions..from command line bound4h Shell Programming and Scripting 3 01-24-2011 10:52 PM



All times are GMT -4. The time now is 02:43 AM.