1

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.

3 Answers 3

3
# read from file1
read LINE

# write to file2 line we just readed
echo $LINE
Sign up to request clarification or add additional context in comments.

6 Comments

do you mean, in the script I can refer file1 as LINE?
no, LINE is a name of variable you read to, since you have replaced stdin with file input - you don't have to use any specific name to refer to input file, you just read it line by line
so, previously I used filename in the sed command : sed -i 's/somePattern/replacedPattern/g' $2. How can I change this command with LINE?
echo $LINE | sed 's/somePattern/replacedPattern/g'
okay. I got it. However, what should I do if I am going to make several changes before I write it to the output file? should I make an temp file? if so, how can I make an temp file in shell?
|
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.