Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store result of tail command in variable?

Tags:

linux

shell

tail

I am developing application in MEAN stack. I want to create a script of image resizing & cropping as a background process when new image uploads to server.

Script watches new image uploads in folder and crop it.

I preferred way of Linux shell scripting as daemon.

I have used following idea to accomplished tasks. - New image uploads on server it writes in photolog.txt file, where I can grab images line by line. - I read photolog.txt in watch.sh shell scripting file. - It Iterates through line by line, until it reaches to the EOL. - Again new file arrives it will append at EOL. - I manage to get updated file by tail command, and get the latest add file display in command-line. Upto this code it works charm.

Now I am successfully grab the image list of newly added file on server. but main issue is that I fails to store output of tail command in variable, and it must to me because whatever output I get it is full path of filename and it will use in imagemagick crop command.

Imagemagick center crop with scale an image.

convert -define file-type:size=widthxheight original_filename -thumbnail 120x120^ -gravity center -extent 100x100 resize_filename

watch.sh

#!/bin/bash
path="/var/www/html/AppBite/trunk/photolog.txt"
cat $path | \

until false
do
    # If file exists 
    if [[ -f "$path" ]]
    then    
        while IFS= read -r photo
        do
            imageFormat=`identify $photo | awk '{print $2}'`
            imageScale=`identify $photo | awk '{print $3}'`
            echo "$photo $imageFormat $imageScale"
        done
    fi
    # Continous monitor file changes via commandline
    tail -f $path
done

I am successfully grab command-line output but I am not able to store value in variable, for next use imagemagick image processing command.

or suggest me other way to continuous monitoring folder to get newly added file list.

like image 448
Dipak Avatar asked Jul 06 '16 10:07

Dipak


1 Answers

Since tail -f doesn't terminate, you don't want to capture its output in a variable. But since you call it in a loop anyway, call it over and over like this:

OUT=`tail "$path"`

Or using the modern syntax:

OUT=$(tail "$path")
like image 146
alexis Avatar answered Nov 19 '22 20:11

alexis