To Write a Shell script that takes two arguments.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To Write a Shell script that takes two arguments.
# 1  
Old 04-05-2007
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 noSmilief 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 01:02 PM..
# 2  
Old 04-05-2007
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  
Old 04-05-2007
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  
Old 04-05-2007
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 noSmilief 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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

4. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

5. Shell Programming and Scripting

Need a shell script which takes two inputs and copy the files from one directory to other

Hi, I am using solari 10 OS which is having bash shell. I need a shell script which takes user home directory and name of the file or directory as a input and based on that copy the files accordingly to the other directory. example:I hava a machine1 which is having some files in a... (8 Replies)
Discussion started by: muraliinfy04
8 Replies

6. UNIX for Dummies Questions & Answers

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (5 Replies)
Discussion started by: ajaira
5 Replies

7. UNIX for Advanced & Expert Users

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

8. Shell Programming and Scripting

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

9. Shell Programming and Scripting

shell script takes long time to complete

Hi all, I wrote this shell script to validate filed numbers for input file. But it take forever to complete validation on a file. The average speed is like 9mins/MB. Can anyone tell me how to improve the performance of a shell script? Thanks (12 Replies)
Discussion started by: ozzman
12 Replies

10. UNIX for Dummies Questions & Answers

Write a script that takes 2 arguments

hi i would be grateful for some help on the following q. "Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top ( the first line) of the file named in your... (6 Replies)
Discussion started by: mopimp
6 Replies
Login or Register to Ask a Question