I am passing the file filename to a program and I have to use the "<" sign:
program < filename
Inside the program I would like to assign the string "filename" to a variable var.
I can't do that with "var=$(echo $1)" because of the "<" sign.
Removing the "<" sign would work for echo, but not for the rest of the program.
Can someone help me sorting this out, using echo or whatever command is suitable?
thanks for your reply
Using var="$1" I get an empty line when trying to display the variable (echo $var)
It works if I run the program with:
program filename
but as I said I have to use a "<" sign
if "$1" has some data in it - assume it is not unset - then var="$1" places data in to the variable "var". Okay?
You seem to want to be able to echo to the screen, not use echo to write to a variable from a child process - which is what I first gave you as a workaround.
This writes to the console.
Is this what you want?
It appears that you want the contents of file name to be the stdin of the program (the < redirects the stdin of program and feeds the contents of the file into it), and you also want the name of the file to be a parameter available in program.
assuming that program expects a filename as the first parameter then
should achieve what you want
The problem ist that $n (for n=1,2,...) denotes the first (second, ...) positional argument. In a construct like "program < file" "file" is not an argument but the "< file" creates an input stream consisting of the content of "file". A program presented with input this way will only recognize some input to its <stdin> but will not know where this is coming from. Otherwise one would have to make (different) provisions for the call "program < file" and "program < /dev/somedevice" or "program < <userinput>". But, contrary, the difference in working with a device, a file or whatever is taken care of by the operating system itself. It will take all these different data sources and turn them into "streams". Note, btw., that you can treat a file like a "clotted datastream" and a datastream like a file. This is one of the biggest avantages of UNIX designwise IMHO.
To the problem of the threadstarter:
You can always solve problems by adding another layer of indirection. ;-))
Seriously: create a small script which does only call the program itself. This script gets a positional parameter which would be the input files name:
If your program awaits positional parameters too you have to modify this wrapper script a bit:
If "program" is another script it would be even easier to modify it to accept an additional parameter "inputfile" and use it instead of using a pipeline. In this case post your script and we'll see how to do it if you can't do it by yourself.
I am afraid I haven't given enough details about my case.
I will try to be more clear posting what I am doing.
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!
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)
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)
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)
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)
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)
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)
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)
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)
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)