Read from file in shell script

The shell script(readfile.sh) to read line by line from the file (input.txt):
[code language=”shell”]
#!/bin/sh
while read line
do
inputargs=($line)
echo "Reading…"
echo "arg1: ${inputargs[0]}"
echo "arg2: ${inputargs[1]}"
done < "input.txt"
[/code]

The input.txt file
[code language=”text”]
one apple
two oranges
three bananas
[/code]

To run the script readfile.sh
[code language=”shell”]
chmod +x readfile.sh
./readfile.sh
[/code]

The output from running the readfile.sh
[code language=”text”]
Reading…
arg1: one
arg2: apple
Reading…
arg1: two
arg2: oranges
Reading…
arg1: three
arg2: bananas
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search