usage of echo with standard input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting usage of echo with standard input
# 8  
Old 10-27-2008
er, you are not passing any parameters to AddTim, you are redirecting it's input stream.
I take it that filename-0.5_s.dat contains a line with X in it somewhere, which you want replaced with X, TIME=0.5?

if so, then
Code:
sh AddTime filename-0.5_s.dat <filename-0.5_s.dat >filename-0.5_s.out

should achieve that.
# 9  
Old 10-27-2008
The > filename-0.5_s.out
creates a blank file before anything else happens, like running your script.
I am assuming that the sed command read from stdin as well.
Code:
#!/bin/sh
# usage AddTim filename
#!/bin/sh
awk '{print $1; exit}' "$1" | read one
time=$(echo $one | cut -d "-" -f2 | cut -d "_" -f1)
sed "s/X/X, TIME=$time/" "$1" > tmp.tmp
mv tmp.tmp "$1"

This parallels the logic in your code without using stdin. There are probably much better solutions. Wempy's solution will work but requires a manual rename of the resulting output file.
# 10  
Old 10-27-2008
a slightly different approach:
Code:
#!/bin/bash
fn=$1
time=$(echo $fn | cut -d "-" -f2 | cut -d "_" -f1)
sed "s/X/X, TIME=$time/" $fn

call that with
Code:
script filename-0.5_s.dat >filename-0.5_s.out


Last edited by wempy; 10-27-2008 at 12:46 PM.. Reason: updated
# 11  
Old 10-27-2008
Quote:
Originally Posted by calandrea
I want to add the time information contained in the filename inside the file using the command:
sh AddTim < filename-0.5_s.dat > filename-0.5_s.out
where AddTim is:
#!/bin/sh
time=$(echo $1 | cut -d "-" -f2 | cut -d "_" -f1)
sed "s/X/X, TIME=$time/"

Unfortunately, the resulting time information is blank!
Ok, lets go through it step by step, a few basics first:

"$1" is the first positional argument. If you call a program "program arg1 arg2" then "arg1" is the content of "$1", the content of "$2" would be "arg2", etc.. If you give only 2 arguments "$3", "$4", etc. will all be blank.

The positional arguments will never present any input to a program but are adjustments to its workings: you could give a filename to a program, but in itself this is not input! Input is when this file is read but to do this the program would have to open and read the file, thus creating the input. The filename itself is only a "calibration" directing this mechanism to the right file, not the input itself. This is an important difference.

Second: you can visualize a UNIX process to be like a garden hose: on one side you pour something in, inside happens something or not, on the other side something pours out. Consider the following command:

Code:
program1 | program2 > file

program1 creates some stream of data. It could be redirected to a file via "program1 > file", but firstly the data pours out of the <stdout> channel of program1. From there it would go to the screen per default, because this is the device this <stdout> is connected to per default. "program1 > file" would simply change this connection from screen to a file named "file". Instead of a file we use another redirection: the pipeline. The pipeline tells the OS to call another program "program2" and connect the <stdin> of this process with the <stdout> of the first process. Similar for the the second redirection, which redirects the <stdout> of program2 to some file.

To stay with our analogy: we have connected two garden hoses (say, a green one and a red one, because the programs are different ones) but inside both of these hoses there is no information about where the content running through them comes from. Also program2 knows nothing about what happens with its output, if it goes into another hose (program) or the dumpster (file).

So, while the pipeline is like the content of the hose, the parameters are dealing with the properties of the hoses themselves: lets say one hose enriches the content running through it with something and the positional parameter would be the adjustment knob controlling how much of the substance is mixed into the stream.

Now, after this rather lengthy theoretical explanation, back to your problem:

Of course the expression is blank because you gave the script no positional parameter - you didn't turn the adjustment knob - you just told the operating system where to get the input from and where to put the output at. This information - because being irrelevant - never reached the script.

But there is a solution, which wempy as well as i did give you: make the filename not only a redirection but also a positional parameter. You can do this by a wrapper script which takes care of the ambiguity so you have to enter the filename only once.

Let the code of AddTim stay like it is (i haven't checked if it is correct, but lets just suppose that it is) and write a second script, called AddTimWrapper. I supposed that your input files are all called ".dat" and the corresponding output files ".out", if this is not true just modify the script a bit:

Code:
#!/bin/sh

fInput="$1"
fOutput="${fInput%%.dat}.out"  # this is just a clever way to subract ".dat" and add ".out"

AddTim "$fInput" < "$fInput" > "$fOutput"

exit $?

Now call the script not by calling "AddTim" but by "AddTimWrapper file" where "file" is a file named "<something>.dat".

I hope this helps.

bakunin
# 12  
Old 10-27-2008
thank you

thanks all of you guys for your replies.
I could now implement the time successfully.
It was also helpful to learn more about scripts.
# 13  
Old 10-27-2008
Code:
$ cat filename-0.5_s.dat
X
$ { rm filename-0.5_s.dat&&sh AddTim "$_">filename-0.5_s.dat;}<filename-0.5_s.dat
$ cat filename-0.5_s.dat
X, TIME=0.5

And, of course, you don't need all that echo, cut ... commands, but that's another problem.
# 14  
Old 10-27-2008
Or even:

Code:
{ rm filename-0.5_s.dat&&sh AddTim "$_">"$_";}<filename-0.5_s.dat

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

2. Shell Programming and Scripting

standard input and cron

I have a program that requires the user to enter input values while it is being run for example in bash ... ... .. echo "Enter your input" read input echo $input ... ... ...I need to schedule this program with crontab, hence a problem, cronjobs run in the background, any ideas on how to... (10 Replies)
Discussion started by: walforum
10 Replies

3. Shell Programming and Scripting

Reading from standard input

So, I am new to shell scripting and have a few problems. I know how to read from standard input but I do not know how to really compare it to say, a character. I am trying to compare it to a character and anything exceeding just a character, the user will get an output message, but the program... (7 Replies)
Discussion started by: Bungkai
7 Replies

4. Shell Programming and Scripting

Reading Standard Input

Hello, I am new to scripting. How do I read multiple lines from the command line? I know read reads one line, but if I have to read multiple lines, how should I do? Thanks, Prasanna (4 Replies)
Discussion started by: prasanna1157
4 Replies

5. Shell Programming and Scripting

Send data to standard input

Hello, I'm writting a korn script that executes a daemon in a remote server. The problem is that daemon doesn't go background until it receives an enter from the standard input, and it maintains the rsh opened until it get it. I'm looking for the best (efficient and elegant) way to do send the... (3 Replies)
Discussion started by: nefeli
3 Replies

6. Solaris

standard input

Please give me any example for standard input in Solaris. (6 Replies)
Discussion started by: karman0931
6 Replies

7. Shell Programming and Scripting

Command Output to Standard Input

Hi All, How do I provide the output of a command to another command which is waiting for an input from the user ? Ex : I need to login to a device via telnet. In the script, initially I use the "read" command to get the IP Address, Username and Password of the device from the user. Now,... (1 Reply)
Discussion started by: sushant172
1 Replies

8. Shell Programming and Scripting

change standard input ?

Dear... I have a scrpit that contains multiple read command.... when I run the script I have to enter 3 variables so that I can get the output.. but, I dont want to put those 3 inputs manually every time... I want to make a shell that reads the 3 inputs from a file. the script name is... (4 Replies)
Discussion started by: yahyaaa
4 Replies

9. Shell Programming and Scripting

How to copy from standard input

I tried copy the output files from find command into a directory. Example, find / -name core 2>/dev/null | xargs cp???? I have known that we can use xargs to execute command lines from standard input but how to use it in this case. Or I can do something besides xargs. (2 Replies)
Discussion started by: lalelle
2 Replies

10. Shell Programming and Scripting

standard input

how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
Discussion started by: Jariya
1 Replies
Login or Register to Ask a Question