LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   get a var from a text file and evaluate it against a var (http://www.linuxquestions.org/questions/linux-general-1/get-a-var-from-a-text-file-and-evaluate-it-against-a-var-4175456577/)

zimbot 04-02-2013 04:02 PM

get a var from a text file and evaluate it against a var
 
Friends,

I am getting jpgs from a webcam and I wish to know if I Stop getting images
so I get the name of the newest jpg by
img=`exec ls -t | sed -n 2p`
( and that works )
andI wish to store that file name in a test file with
echo $img > /root/00_script/imgNm.txt
( and that seems to work )

but reading the txt file ( imgNm.txt ) back into a var and doing an evluation for are the they same. is NOT working
if [[ $img == $LstImgNm ]];then
echo 'SAME ALERT'
fi
i have do -e & = [[ ]] & [ ]
but i think the problem is getting the 2nd var from the txt file.

and I think that is because I am Not etting the contents of the txt file into the 2nd var $LstImgNm ( last image name )
I have tried a few diffrent ways.


#!/bin/bash
##
## for the mdealer 3.27.2013 js
### attempts at the webpage img
#
#
##############################
####
webD1=/mnt/expan/webcam_ftp
#webD2=/mnt/expan/webcam_img
######### new loc -- may change again for website
webD2=/var/www/html

cd $webD1
### echo pwd
LstImgNm=<$(/root/00_script/imgNm.txt)
#LstImgNm= cat /root/00_script/imgNm.txt
#img=ls -t | sed -n 2p
img=`exec ls -t | sed -n 2p`
## must do the bacTik exec cmd bacTic
echo current is $img
echo previous was $LstImgNm
if [[ $img == $LstImgNm ]];then
echo 'SAME ALERT'
fi
echo current is $img
echo previous was $LstImgNm
## populate the tst file
echo $img > /root/00_script/imgNm.txt
echo stage 2
#cp $webD1/$img $webD2/new.jpg
exit


whe I run it i see
[root@Mixer 00_script]# ./mv_webcam2.sh
/root/00_script/imgNm.txt: line 1: snap_c1_130402_142528.jpg: command not found
/root/00_script/imgNm.txt: line 1: snap_c1_130402_142528.jpg: command not found
./mv_webcam2.sh: line 16: $(/root/00_script/imgNm.txt): ambiguous redirect
current is snap_c1_130402_145529.jpg
previous was
current is snap_c1_130402_145529.jpg
previous was
stage 2

---------------------

and of course I should see
current is snap_c1_130402_145529.jpg
previous was snap_c1_130402_145529.jpg
or at leaste
current is snap_c1_130402_145529.jpg
previous was something.jpg

-------------

thanks

zimbot 04-02-2013 04:12 PM

also let me say the contents of the txt file is 1 line
snap_c1_130402_152529

and if I change my cde like this

#!/bin/bash
##
## for the mdealer 3.27.2013 js
### attempts at the webpage img
#
#
##############################
####
webD1=/mnt/expan/webcam_ftp
#webD2=/mnt/expan/webcam_img
######### new loc -- may change again for website
webD2=/var/www/html

cd $webD1
### echo pwd
LstImgNm=snap_c1_130402_152529.jpg
#LstImgNm=<$(/root/00_script/imgNm.txt)
#LstImgNm= cat /root/00_script/imgNm.txt
#img=ls -t | sed -n 2p
img=`exec ls -t | sed -n 2p`
## must do the bacTik exe
## must do the bacTik exec cmd bacTic
echo current is $img
echo previous was $LstImgNm
if [[ $img = $LstImgNm ]];then
echo 'SAME ALERT'
fi
echo current is $img
echo previous was $LstImgNm
## populate the tst file
#echo $img > /root/00_script/imgNm.txt
echo stage 2
#cp $webD1/$img $webD2/new.jpg
exit

---------------

then when i run i get

./mv_webcam2.sh
current is snap_c1_130402_152529.jpg
previous was snap_c1_130402_152529.jpg
SAME ALERT
current is snap_c1_130402_152529.jpg
previous was snap_c1_130402_152529.jpg
stage 2

----------------------

so it MUST be the
reading a txt file nto the var.
LstImgNm=<$(/root/00_script/imgNm.txt)

thanks

chrism01 04-02-2013 11:51 PM

Code:

var=$(cat filename)
& ensure you have no trailing blank lines in the file.

Incidentally, if you add 'set -xv' as the 2nd line, you can see what's happening (what the parser sees and what it does with it)
Code:

#!/bin/bash
set -xv


kooru 04-03-2013 02:59 AM

Hi zimbot,

please next time use tag code, so it's easy read your code :)


All times are GMT -5. The time now is 04:58 AM.