You are not logged in.

#1 2010-09-21 09:33:14

kgas
Member
From: Qatar
Registered: 2008-11-08
Posts: 718

[solved]variable using ~ from a file

To put the things in short, I am trying to share some files/directory using quickserve and want to pass the file list thro' another file. before actual sharing I am trying to  test the actual file's existence but it fails with ~ / $HOME options.

File : ToExport.txt
contents:
~/Documents/File1.dot
~/Documents/File2.ods
/mnt/backup

the said file is passed as an argument to the bash script

Test function ...

for isvalid in $(cat "$1")
       do
           if [ -e "$isvalid" ]
           then
               echo "$isvalid passed for sharing"
           else
               echo "$isvalid does not exisits"
               exit 4
           fi
       done
This fails. If I replace ~ with actual path it works. 

(It may be easy to replace the entire list with the actual path by the simple search & replace funtion but the indention is to use the short form  ~ or $HOME instead of typing full path.

your inputs/correction are welcome

Last edited by kgas (2010-09-28 17:11:41)

Offline

#2 2010-09-21 10:26:30

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved]variable using ~ from a file

Use a parameter expansion to replace the tilde with $HOME (which will be properly expanded in the test).

while read file; do
  if [[ -e ${file/\~/$HOME} ]]; then
    echo "$file is valid"
  else
    echo "$file is not valid"
  fi
done < "$1"

Last edited by falconindy (2010-09-21 10:26:48)

Offline

#3 2010-09-21 10:29:22

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [solved]variable using ~ from a file

This should work:

#!/bin/bash
#: Description : Checks file list to see files contained exist/are empty

fileList=/home/tom/fileList

## Go through each file in the fileList
while read aFile
do
  ## expand tildes (~)
  eval aFile=$aFile
  
  ## check file exists
  if [ -e $aFile ]
  then

    ## check file is non-empty
    if [ -s $aFile ]
    then
      printf "%s is a file.\n" $aFile
    else
      printf "%s is an empty file.\n" $aFile
    fi
    
  else ## file does not exist
    printf "%s does not exist.\n" $aFile
  fi
done < <( cat $fileList )

That code is a little more readable too. You where using the variable "isValid" for a file name.

Last edited by BaconPie (2010-09-21 10:29:51)

Offline

#4 2010-09-21 10:48:46

kgas
Member
From: Qatar
Registered: 2008-11-08
Posts: 718

Re: [solved]variable using ~ from a file

Thanks for the feed back.
@falconindy : works fine
@BaconPie :works, I will do the empty file/availabilty test for the passed file and in a loop i indent to check the availability of the file contents which are really shared.
   Note eval var1=$var2 did not work. After spending some time I found a work around.

while read isfile; do
    if [ -e $(eval echo $isfile) ]; then
     echo  "$isfile is a valid"
  else
    echo "$isfile is not valid"
  fi
done < "$1"

works as expected. with this relative path can also be given with dot notation ie ../../ etc.

Last edited by kgas (2010-09-28 17:11:06)

Offline

Board footer

Powered by FluxBB