From the course: Learning Linux Shell Scripting

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Reading files

Reading files - Linux Tutorial

From the course: Learning Linux Shell Scripting

Start my 1-month free trial

Reading files

- [Instructor] We can read a file into a script using the read command and redirection. First, let's create a text file so we can read it later. Touch names.txt and atom names.txt Enter the following name or your own if you like. Place each name on it's own line. There is no need for a shebang since, this is a text file and not a script. So Abel, Maria, Richard, Tom, and Rene. Then we do a control S to save it and let's return to the terminal. Now let's create our script. We do touch reader.sh, change mode 755, reader.sh, and finally atom reader.sh Enter our shebang /bin/env space bash and we create a counter, so COUNT=1 and then we type in while IFS=, and these are single quotes with nothing in between them space read -r and LINE in all caps. IFS is the internal field separator. It tells Linux how to parse the field of the line we are reading. We aren't doing anything special so we don't really need this so we set it to an empty string. The -r on the read command says do not allow…

Contents