Generalities don't help.
If ./program contains:
and input.in contains:
then the command:
will create output.out as an empty file.
If ./program contains:
and input.in is not an empty file, then output.out will not be an empty file.
I repeat: What does ./program do? What is in input.in?
program file contains:
echo $2 $1 input.in file contains:
a b
When doing:
The output.out was empty
However, when doing: ./program `cat input.in` > output.out
The output.out has the expected output which was b a .
I feel so confused about that. What is the difference?
I feel so confused about that. What is the difference?
Quite simple: "$1" (2, 3, ...) are variables which are filled with COMMANDLINE ARGUMENTS, whereas what you are redirecting to/from is INPUT and OUTPUT respectively. These are simply two different things. If you tell someone to count all persons coming through door 1 and there will be 100 persons coming through door 2 he will still come up with 0.
What your program does is: take the first two arguments and print them to "stdout" in reversed order. Nothing at all is done with what comes from "stdin". Therefore the output file is empty: you passed no commandline arguments, therefore "$1" and "$2" are empty and this emptyness is written to the file "output.out".
Why your second try worked is simple: "cat input.in" is a useless use of cat, but will print the contents of input.in at the commandline, because it is surrounded by backticks (which should not be used either). Therefore what you wrote was equivalent to
because this is what the shell will evaluate the whole backticks-construction to. This will fill the commandline variables "$1" and "$2" with some value ("a" and "b") and therefore something gets printed to "output.out" - just as i said above.
Wow, that's excellent! Now I understand the differences. But, how should I modify my program to get the standard input from input.in?
Change the contents of program from:
to:
or, if the contents of input.in could contain characters with special meaning to the shell (including backslash (\) or starting with a minus sign (-): IFS="" read -r a1 a2 junk; printf "%s %s\n" "$a2" "$a1"
This User Gave Thanks to Don Cragun For This Post:
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)
Hi Geeks,
I am relatively new to Unix. Trying out to achive a shell script by hard learning. Here is my requirment.
1. I have to search for specified strings that are given in .csv file in the directory to find the files for matching strings in the .csv file.
2. If match is found, copy... (1 Reply)
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)
Hi
I am trying to write a shell script which should take the file as standard input. As file(content and name both) will change for each run. It should read the file line by line. with each line I have to perform certain operation.
For example
I have i file foo, it looks like
/usr/doc/abc... (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)
hi folks
how do i output contents of file in standard output.
in my script, i say
x=`cat filename'
echo $x
below is the actual file
***********
asda afdf fdf sdf dsfsdfsd fds dsfdfsdfsdg ssgd sgdg
sdfsdgfsdg dgfd gsfd gs sdg sfdg s
in my script, i am trying to output the... (4 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)