Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a folder of 200 satellite files to process. There are three steps in the process and I want to use a shell script under ubuntu to process them. I am using a for loop.

My problem is the filenames. An example is A2013290123000.L1A_LAC.Ireland.hdf.

I can create variables ie.

DATE=A2013
DATE1=`date +%j` #gives me the number of days since jan 1st 2013 in example above this would be 290
.L1A_LAC.Ireland.hdf part of the name doesn't change.

My issue is with the middle part of the name, in example above:123000

There is no pattern to this part of the name. It is a time stamp but varies with each file.

I want to create a variable in the script such as

TIMESTAMP=$DATE$DATE1$DATE2

Where DATE2= the middle part of the file name.

Is it possible to specify this variable as a wild card something like:

DATE2=$*

I hope to have the value of the variable TIMESTAMP= A2013209123000

#!/bin/sh

#set -e

set -x #debug mode

<<comment
export OCSSWROOT=/home/seadas/seadas-7.0/ocssw
source $OCSSWROOT/OCSSW_bash.env
export PATH=$PATH:/home/seadas/seadas-7.0/bin
comment

DATE=A2013
DATE1=`date +%j`
DATE2=$*
TIMESTAMP=$DATE$DATE1$DATE2
LOCATION_NAME=Ireland

DATADIR=/home/MODIS
L2_DIR=/home/MODIS/L2
GEO_FILE_DIR=/home/MODIS/GEO
L1B_DIR=/home/MODIS/L1B
SCRIPTDIR=/home/seadas/seadas-7.0/ocssw/run/scripts
FILTERDIR=/home/seadas/seadas-7.0/ocssw/run/data/common
FUNCTION=/home/seadas/seadas-7.0/ocssw/run/bin/linux_64

HDFFILE=$TIMESTAMP.L1A_LAC.$LOCATIONNAME.hdf
GEOFILE=$LOCATION_NAME-$TIMESTAMP.GEO
LACFILE=$TIMESTAMP.L1B_LAC
HKMFILE=$TIMESTAMP.L1B_HKM
QKMFILE=$TIMESTAMP.L1B_QKM
L2FILE=$TIMESTAMP.L2.hdf


for i in `ls -r $DATADIR`

do
    echo "Start a Process for file $i";

'Generating geolocation file' $SCRIPTDIR/modis_GEO.py -d $DATADIR/$HDFFILE -o $GEO_FILE_DIR/$TIMESTAMP.GEO --threshold=95;

'Generating L1B file' $SCRIPTDIR/modis_L1B.py $DATADIR/$HDFFILE $GEO_FILE_DIR/$TIMESTAMP.GEO -o $L1B_DIR/$LACFILE -k $L1B_DIR/$HKMFILE -q $L1B_DIR/$QKMFILE;

'Generating L2 product'$FUNCTION/l2gen ifile=$L1B_DIR/$LACFILE geofile=$GEO_FILE/$TIMESTAMP.GEO par=$FILTERDIR/msl12_defaults.par ofile=$L2_DIR/$L2FILE resolution=-1 l2prod="default,sst,qual_sst,qual_sst4,sstref,sst4" filter_opt=0 proc_ocean=1 gas_opt=15;

echo "it $i is finished..." 

    echo "\n"
done
share|improve this question
    
I could answer if I could understand the question. What exactly are you trying to do? You have a filename A2013290123000.L1A_LAC.Ireland.hdf made up of an A a date (what date is 2012290?) 123000 and .LaA_LAC and you want to tokenise it, is this correct. –  richard Nov 20 '13 at 10:59
    
sounds like a job for a regex, but you will have to tell me what you are trying to do. –  richard Nov 20 '13 at 11:00

2 Answers 2

This does not work as you expect.

  • In general, you should not loop over results of ls because it fails if you have special characters in the filenames (like whitespaces which is probably not the case for your problem);
  • loop over the existing HDF-files and extract the TIMESTAMP from the filenames:

    # assuming you do not have whitespaces etc. in filenames
    for i in $DATADIR/$DATE$DATE1*.L1A_LAC.$LOCATIONNAME.hdf ; do
      if [ ! -f "$i" ] ; then  #check if pattern could be expanded
           break ; 
      fi
      HDFFILE="$i"
      TIMESTAMP=$(basename "$i" ".L1A_LAC.$LOCATIONNAME.hdf")
      GEOFILE=$LOCATION_NAME-$TIMESTAMP.GEO
      LACFILE=$TIMESTAMP.L1B_LAC
      HKMFILE=$TIMESTAMP.L1B_HKM
      QKMFILE=$TIMESTAMP.L1B_QKM
      L2FILE=$TIMESTAMP.L2.hdf
    
      # process $i
      # [...]
    
share|improve this answer

If I understand you correctly, you need to remove everything after the first dot in your file name in order to obtain the time stamp. This parameter expansion should help:

TIMESTAMP="${i%%.*}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.