Posts tagged 'bash'

Luca Lanziani

Read file in chunks in bash

How to read a file in chunks. You can optionally specify the starting point and the chunk size.


function read_file_in_chunks { # FILENAME [START_FROM_LINE] [CHUNK_SIZE]
   FILENAME="$1" && shift
   START_FROM_LINE="$1" && shift || START_FROM_LINE=1
   CHUNK_SIZE="$1" && shift || CHUNK_SIZE=10

   FILE_LINES=$(wc -l < "${FILENAME}")

   while [[ ${FILE_LINE} -le ${FILE_LINES} ]]; do
       CHUNK …

Luca Lanziani

It's a Trap!

I do a big use of shell scripts, and many of them contain a for loop, do you know what normally happen when you press CTRL^C and the script is executing an action inside the loop?

for i in test{6..10}.com; do
    echo "pinging ${i}"; ping -c …