![]() |
Silly bash script programming....
How to read a parameter from console?
Like: ./readBash.sh name then my script print the name out? Thanks! |
$1 is the first argument, $2 is the second one, etc. If you want it all in one string you can use $@ .
|
and $0 is the name of program or script.
|
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:
|
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 |
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:
|
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 ]Then echo $1 shift echo $1 will print "This is". |
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:
|
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 |
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:
|
As stated before if you run:
Code:
echo $1Try reading a guide on bash, I recommend this one: http://tldp.org/LDP/abs/html/ |
It works!
$1 always print the first input item on the console. No matter where it is in the script Quote:
|
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 06:14 AM. |