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
passing runtime arguments to a shell script... santy Shell Programming and Scripting 10 01-09-2009 10:47 PM
Is there a limit to the no. of arguments to a shell script ? hidnana Shell Programming and Scripting 4 03-17-2008 12:19 PM
shell script takes long time to complete ozzman Shell Programming and Scripting 12 02-05-2007 11:25 PM
Write a script that takes 2 arguments mopimp UNIX for Dummies Questions & Answers 6 03-24-2006 06:16 PM
Shell script with arguments sankar6254 Shell Programming and Scripting 3 12-22-2003 09:21 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 04-05-2007
bobby36 bobby36 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 10
Question To Write a Shell script that takes two arguments.

How to write a shell script tht takes two arguments.The first being a line of text,the second being your newly created file(x nof lines and content of ur choice).

The script should take the first argument and insert it into the very top(the first line)of the file named in your second argument.

Note : the file must contain the original name.

Last edited by bobby36; 04-05-2007 at 12:02 PM..
  #2 (permalink)  
Old 04-05-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Sounds like homework....
Code:
#!/bin/ksh
# addline.sh
echo "$1" > tmp.$$
cat "$2" >> tmp.$$
mv tmp.$$ "$2"
usage: addline "stuff to write on line one" myfile
  #3 (permalink)  
Old 04-05-2007
bobby36 bobby36 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 10
Thanks Jim, I appreciate for your quick response.I know this is a basic script but I'm alien to scripting and more over I'm into testing so dont even know abcd of scripting.I was given a task in shell scripting to complete.

The off uses bash not ksh.So could you please write it in bash.I really appreciate for your help.I have got 5 more simple questions hope you will answer them aswell.
  #4 (permalink)  
Old 04-05-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 bobby36
How to write a shell script tht takes two arguments.The first being a line of text,the second being your newly created file(x nof lines and content of ur choice).

The script should take the first argument and insert it into the very top(the first line)of the file named in your second argument.

Note : the file must contain the original name.

This will work in any POSIX shell (including bash, ksh, etc.):
Code:
{
  printf "%s\n" "$1"
  cat "$2"
} > tempfile.$$
mv tempfile.$$ "$2"
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 11:42 AM.


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