LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Silly bash script programming.... (http://www.linuxquestions.org/questions/programming-9/silly-bash-script-programming-620642/)

ArthurHuang 02-12-2008 02:20 PM

Silly bash script programming....
 
How to read a parameter from console?

Like: ./readBash.sh name

then my script print the name out?

Thanks!

PatrickNew 02-12-2008 02:28 PM

$1 is the first argument, $2 is the second one, etc. If you want it all in one string you can use $@ .

dive 02-12-2008 02:34 PM

and $0 is the name of program or script.

ArthurHuang 02-12-2008 02:59 PM

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 (Post 3054842)
$1 is the first argument, $2 is the second one, etc. If you want it all in one string you can use $@ .


PatrickNew 02-12-2008 03:10 PM

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

ArthurHuang 02-12-2008 03:20 PM

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 (Post 3054894)
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


PatrickNew 02-12-2008 03:39 PM

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".

ArthurHuang 02-12-2008 03:43 PM

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 (Post 3054928)
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".


PatrickNew 02-12-2008 04:12 PM

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


ArthurHuang 02-13-2008 08:31 AM

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 (Post 3054963)
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



H_TeXMeX_H 02-13-2008 09:26 AM

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/

ArthurHuang 02-13-2008 10:09 AM

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 (Post 3055731)
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/


H_TeXMeX_H 02-14-2008 07:56 AM

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.


All times are GMT -5. The time now is 07:58 AM.