LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   checking for a file: problem with -e (https://www.linuxquestions.org/questions/linux-newbie-8/checking-for-a-file-problem-with-e-829785/)

aspn 09-01-2010 01:53 PM

checking for a file: problem with -e
 
Please advice why check for presence in the dir could be failing with -e, when file is definitely present, as illustrated in this example (not a real logic, but illustrates my problem):

#!/bin/bash

d=my_dir
for (( i=1; i < 10; i++))
do
j4=`printf "%04d" $i`
f214=$d/s_2_1_${j4}_qseq.txt
if [ ! -e "$f214"* ] ; then
ls -l "$f214"
fi
done

$> -rwxrwxrwx 1 sbsuser domain_users 720258032 Jun 14 11:21 /bruno/RTAOutput/100512_SN101_0440_AFC202J1/Data/Intensities/BaseCalls/s_2_1_0001_qseq.txt

Thanks in advance

smoker 09-01-2010 02:36 PM

Try adding echo "$f214" before the if. Is it what you expect ? Where should the glob be, in your if ?

gwalli 09-01-2010 02:39 PM

Not sure what your problem is. I cannot reproduce, but the asterisk
in:
if [ ! -e "$f214"* ]
looks suspicious.
Try:
if [ ! -e "$f214" ]
instead, and see if that helps.

Here is my test script to try to reproduce:
$ cat ~/lq.sh
#!/bin/bash
d=/tmp
echo These files exist:
ls -l ${d}/*qseq.txt*
echo Doing the loop:
for (( i=1; i < 10; i++))
do
f214=$(printf "$d/s_2_1_%04d_qseq.txt" ${i})
[ ! -e "$f214"* ] && ls -l "$f214"
[ -e "$f214" ] && echo ${f214} exists!
done
$ ~/lq.sh
These files exist:
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0001_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0002_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0003_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:32 /tmp/s_2_1_0003_qseq.txt3
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0005_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0006_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0007_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0008_qseq.txt
-rw-rw-r-- 1 walli walli 0 2010-09-01 14:36 /tmp/s_2_1_0009_qseq.txt
Doing the loop:
/tmp/s_2_1_0001_qseq.txt exists!
/tmp/s_2_1_0002_qseq.txt exists!
/home/walli/lq.sh: line 9: [: /tmp/s_2_1_0003_qseq.txt: binary operator expected
/tmp/s_2_1_0003_qseq.txt exists!
ls: cannot access /tmp/s_2_1_0004_qseq.txt: No such file or directory
/tmp/s_2_1_0005_qseq.txt exists!
/tmp/s_2_1_0006_qseq.txt exists!
/tmp/s_2_1_0007_qseq.txt exists!
/tmp/s_2_1_0008_qseq.txt exists!
/tmp/s_2_1_0009_qseq.txt exists!

aspn 09-02-2010 11:20 AM

- solved: extra space
 
You're right, * is the problem, which I noticed only after copy-paste; extra space in my editor (Notepad++) (ASCII 32) turned into * (42). Weird.
Removing 'star' (extra space) solved the problem.
Thank you guys.


All times are GMT -5. The time now is 06:28 PM.