user1988385
user1988385

Reputation: 2869

using standard input in shell script

I always run my shell with a command:

./shellName file1 file2

So, in the shell, I can refer file1 as $1 and file2 as $2. However, if I want to run the shell with this command

./shellName < file1 > file2

I tried to read the file1 as standard input and file 2 as standard output. How can I refer file1 and file2 in the shell script? Can I still use $1 and $2? Thanks in advance.

Upvotes: 1

Views: 1682

Answers (3)

Stefan Georg
Stefan Georg

Reputation: 1

If you directing files in, I don't think that the script has the variables available to them, rather the text is read from file1 and used as input for the script and anything outputted will end up in file2

Upvotes: 0

zed_0xff
zed_0xff

Reputation: 33217

# read from file1
read LINE

# write to file2 line we just readed
echo $LINE

Upvotes: 3

Guru
Guru

Reputation: 16974

Any read statement in your script will read from file1 and any echo/print statement will write the output to file2. $1 and $2 will be empty

Upvotes: 1

Related Questions