Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Accepting filename as command line param and writing to it

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 02-06-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 August 2015, 10:24 AM EDT
Location: The Netherlands
Posts: 7,732
Thanks: 129
Thanked 549 Times in 516 Posts
Try this:


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

Read some scripting tutorials or books.

Regards
Sponsored Links
    #3  
Old Unix and Linux 02-06-2008
manas_ranjan's Unix or Linux Image
manas_ranjan manas_ranjan is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 14 April 2015, 5:25 AM EDT
Location: Amsterdam
Posts: 280
Thanks: 13
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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 02-06-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 August 2015, 10:24 AM EDT
Location: The Netherlands
Posts: 7,732
Thanks: 129
Thanked 549 Times in 516 Posts
Show your entire script. Have you make your script executable with chmod?

Regards
Sponsored Links
    #7  
Old Unix and Linux 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 Linux or Unix Question

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 pass a filename as a command line argument ektubbe Shell Programming and Scripting 1 05-01-2012 03:25 AM
Accepting command line arguments in bash kristinu UNIX for Dummies Questions & Answers 17 03-16-2012 12:21 PM
Preserving newlines when writing loops on the command line in bash JohnK1 Shell Programming and Scripting 2 03-02-2011 03:42 PM
grep not accepting variable filename bdrelling Shell Programming and Scripting 1 02-20-2011 10:12 PM
Change filename extensions..from command line bound4h Shell Programming and Scripting 3 01-24-2011 09:52 PM



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