LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-12-2008, 02:20 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Silly bash script programming....


How to read a parameter from console?

Like: ./readBash.sh name

then my script print the name out?

Thanks!
 
Old 02-12-2008, 02:28 PM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
$1 is the first argument, $2 is the second one, etc. If you want it all in one string you can use $@ .
 
Old 02-12-2008, 02:34 PM   #3
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,199

Rep: Reputation: 290Reputation: 290Reputation: 290
and $0 is the name of program or script.
 
Old 02-12-2008, 02:59 PM   #4
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks a lot!

Then how to write some words into a file?

I try to use this format :

<<<lineNum=$1
scp lineNum name@/path/
>readme.txt

But I find the bash recognize the scp as commands, not words in readme.txt.


Quote:
Originally Posted by PatrickNew View Post
$1 is the first argument, $2 is the second one, etc. If you want it all in one string you can use $@ .
 
Old 02-12-2008, 03:10 PM   #5
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
I'm not sure what you mean by that. scp handles copying files across networks. If you just want to write the command line to a file try

echo $@ > file

which will overwrite the file with the command line, or

echo $@ >> file

which will append the command line to the file
 
Old 02-12-2008, 03:20 PM   #6
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
What if I want to write multiple lines into the script?
For example, just simply write two lines into a .txt file?

line=xxx
scp line.txt useer@server:/path/





Quote:
Originally Posted by PatrickNew View Post
I'm not sure what you mean by that. scp handles copying files across networks. If you just want to write the command line to a file try

echo $@ > file

which will overwrite the file with the command line, or

echo $@ >> file

which will append the command line to the file

Last edited by ArthurHuang; 02-12-2008 at 03:26 PM.
 
Old 02-12-2008, 03:39 PM   #7
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
I'm still confused about the scp. Are you trying to copy files across a network?

This code

line=xxx
scp line.txt useer@server:/path/

Puts the string xxx into the environment variable line, then copies some file named line.txt over a network. If you want to write the command line to the file with each word on it's own line, that would be like this

Code:
while [ -n $1 ]
do
  echo $1 >> file.txt
  shift
done
This says, while there is a first argument, append it (on a new line) onto file.txt. Then forget the first argument, and shift all the other arguments over. So, if the command arguments where "This is some text"
Then

echo $1
shift
echo $1

will print "This is".
 
Old 02-12-2008, 03:43 PM   #8
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
I had a $200 headache....

Well, actually I want to generate a .txt file by .sh.

I don't need to redirect I/O or using pipe. It should be simple, just generate a data.txt, which contains these two sentenses....

line=xxx
scp line.txt useer@server:/path/

I think your method is to read two lines, then saved into a txt file. That is advanced level for me right now Thanks!

Quote:
Originally Posted by PatrickNew View Post
I'm still confused about the scp. Are you trying to copy files across a network?

This code

line=xxx
scp line.txt useer@server:/path/

Puts the string xxx into the environment variable line, then copies some file named line.txt over a network. If you want to write the command line to the file with each word on it's own line, that would be like this

Code:
while [ -n $1 ]
do
  echo $1 >> file.txt
  shift
done
This says, while there is a first argument, append it (on a new line) onto file.txt. Then forget the first argument, and shift all the other arguments over. So, if the command arguments where "This is some text"
Then

echo $1
shift
echo $1

will print "This is".
 
Old 02-12-2008, 04:12 PM   #9
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Oh, you wanted the literal text

"line=xxx
scp line.txt useer@server:/path/"

to appear in the file. Haha, now I understand.

Just try
Code:
echo "line=xxx" >> file.txt
echo "scp line.txt user@server:/path/" >> file.txt
 
Old 02-13-2008, 08:31 AM   #10
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
That's easy, thanks!
Anyway, seems go back to the old track but different:
What if xxx is read from console?

For example, the script name is readLine.sh, and I want to run it like:

./readLine.sh 999


Quote:
Originally Posted by PatrickNew View Post
Oh, you wanted the literal text

"line=xxx
scp line.txt useer@server:/path/"

to appear in the file. Haha, now I understand.

Just try
Code:
echo "line=xxx" >> file.txt
echo "scp line.txt user@server:/path/" >> file.txt
 
Old 02-13-2008, 09:26 AM   #11
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
As stated before if you run:
Code:
echo $1
from the script it'll print out 999 which you entered in the console. And $2 for the second, $3 for third, etc., and $@ for all.

Try reading a guide on bash, I recommend this one:
http://tldp.org/LDP/abs/html/
 
Old 02-13-2008, 10:09 AM   #12
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
It works!

$1 always print the first input item on the console. No matter where it is in the script
Quote:
Originally Posted by H_TeXMeX_H View Post
As stated before if you run:
Code:
echo $1
from the script it'll print out 999 which you entered in the console. And $2 for the second, $3 for third, etc., and $@ for all.

Try reading a guide on bash, I recommend this one:
http://tldp.org/LDP/abs/html/
 
Old 02-14-2008, 07:56 AM   #13
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Good, glad it works. Also, bash is really quite an easy language to learn, and very powerful too, at least at a high level, you can do many things with just a few simple commands. It took me no more than 1 week to learn the basics, the rest you'll learn as you write more scripts.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script programming problem ArthurHuang Programming 5 01-23-2008 03:24 PM
issues programming a script in BASH gravesb Linux - Software 1 07-07-2005 01:03 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
Bash script programming questions dianea Linux - Newbie 4 03-03-2004 12:17 AM


All times are GMT -5. The time now is 06:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration