-3

I have to create a script who read the NUMBER of lines from any file. I've thought to use the structure do / while. How i can do it?

David Cep
  • 7
  • 1

1 Answers1

0

Syntax to read file line by line on a Bash Unix & Linux shell:

The syntax is as follows for bash, ksh, zsh, and all other shells –

1) while read -r line; do COMMAND; done < input.file

2) The -r option passed to read command prevents backslash escapes from being interpreted.

3) Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed - while IFS= read -r line; do COMMAND_on $line; done < input.file

#!/bin/bash
COUNTER=0
input="/path/to/txt/file"
while IFS= read -r var
do
  echo "$var"
  COUNTER=$[$COUNTER +1]
done < "$input"
echo " $COUNTER "
Hiren Jungi
  • 854
  • 8
  • 20