FIle (directory) test operator (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FIle (directory) test operator (bash)
# 1  
Old 05-18-2009
FIle (directory) test operator (bash)

I'm almost pulling out my hair trying to figure out what's wrong with this... there's no reason I can see that it shouldn't be working. It seems that the code acts as though the conditional statement is true no matter what - I've even tried removing the negation operator, but it always goes into the first block of code whether or not the directory exists. I've also tried switching -d to -e, but it gets the same result. Thanks in advance!

Code:
directory="Test"
echo "Does $directory exist in:"
pwd
if [[ ! -d "$directory" ]];
then
    mkdir $directory
    echo "$directory doesn't exist"
else
    echo "$directory exists."
fi

Output: (when the folder exists)
Code:
Does Test exist in:
/media/WALTER/autosync
mkdir: cannot create directory `Test': File exists
Test doesn't exist

Output: (when the folder doesn't exist)
Code:
Does Test exist in:
/media/WALTER/autosync
Test doesn't exist

# 2  
Old 05-18-2009
Try
Code:
cd /media/WALTER/autosync
ls -l Test

"Test" is another type of file, like a text file for example.
# 3  
Old 05-18-2009
works perfectly for me...

Ran it as a script...
Code:
#!/bin/bash

directory="Test"
echo "Does $directory exist in:"
pwd
if [[ ! -d "$directory" ]];
then
    mkdir $directory
    echo "$directory doesn't exist"
else
    echo "$directory exists."
fi

Output when running script:

Code:
user@aixdev20 $ chmod u+x test.bash
[/home/user]
user@aixdev20 $ ./test.bash
Does Test exist in:
/home/user
Test doesn't exist
[/home/user]
user@aixdev20 $ ./test.bash
Does Test exist in:
/home/user
Test exists.

Padow
# 4  
Old 05-18-2009
Jim's answer makes sense, should have thought of that myself.
Padow
# 5  
Old 05-18-2009
Just tried your code is a script (no header) and then run it using:
Code:
$ bash -x ./testscript

and it worked fine (in Ubuntu Linux).

[code]
/tmp$ bash -x ./testscript
+ directory=Test
+ echo 'Does Test exist in:'
Does Test exist in:
+ pwd
/tmp
+ [[ ! -d Test ]]
+ mkdir Test
+ echo 'Test doesn'\''t exist'
Test doesn't exist
/tmp$ bash -x ./testscript
+ directory=Test
+ echo 'Does Test exist in:'
Does Test exist in:
+ pwd
/tmp
+ [[ ! -d Test ]]
+ echo 'Test exists.'
Test exists.
/tmp$ rmdir Test
/tmp$ bash ./testscript
Does Test exist in:
/tmp
Test doesn't exist
tony@tony-laptop:/tmp$ bash ./testscript
Does Test exist in:
/tmp
Test exists.
/tmp $
[code]
# 6  
Old 05-19-2009
Well, I removed the semi-colon after the conditional statement and now it works perfectly - thank you all for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unknown test operator

O/S solaris 9 shell ksh if then chk_op="WARNING...reboot within the last 24hrs, restarted at `who -r | awk '{print $4$5"@"$6}'`" ; else if ; then last_reboot1=`who -b | awk '{print $4" "$5" "$6}'` last_reboot2='..OK..'`uptime | awk '{print$3" "$4}'` ... (4 Replies)
Discussion started by: squrcles
4 Replies

2. Shell Programming and Scripting

Operator test in bash

Hello, can you please help me because I am totally confused with a simple script: #!/bin/bash ] || ] && echo "Good Morning" ] || ] && echo "Good Night" For me, these two strings are indentical: false || false and there is no point to execute echo command. But the run result is... (5 Replies)
Discussion started by: AndreiM
5 Replies

3. Shell Programming and Scripting

Unknown test operator

Hi , Os - Solaris Shell - Ksh I am getting below error in if condition + id + ./om_wf_complete.sh: gid=4081(Infadmn): unknown test operator + exit 1 if ; then touch /home/odwt/1.0.0/out/oworkflow.dat chmod 777 /home/odwt/1.0.0/out/oworkflow.dat elif ; then touch... (8 Replies)
Discussion started by: nag_sathi
8 Replies

4. Shell Programming and Scripting

Test operator

In my script (currently running on Solaris ) I'm testing for zero size with wild character. There are mutilple files exist in the directory. if then filename=`ls -1tr ${fileformat}.${date}.? | tail -1` else ${BATCH_FATAL:-echo} "$0:ERROR:No file found ${source}/${fileformat}.${date}.?"... (5 Replies)
Discussion started by: gauravgoel83
5 Replies

5. Shell Programming and Scripting

Problem in test file operator on a ufsdump archive file mount nfs

Hi, I would like to ask if someone know how to test a files if exist the file is a nfs mount ufsdump archive file.. i used the test operator -f -a h almost all test operator but i failed file1=ufs_root_image.dump || echo "files doesn't exist && exit 1 the false file1 is working but... (0 Replies)
Discussion started by: jao_madn
0 Replies

6. Shell Programming and Scripting

Solaris test operator -gt

Hello people, I created the below script on HP-UX #!/bin/sh SESSION_NO=`sqlplus -s ${DRBILOUKOS_USER} <<EOF SET HEAD OFF @/export/home/drbiloukos/scripts/sessions_count/no_of_sessions.sql EOF` SESSION_THRESHOLD=250 MAX_SESSIONS=`sqlplus -s ${DRBILOUKOS_USER} <<EOF SET HEAD OFF... (2 Replies)
Discussion started by: drbiloukos
2 Replies

7. Shell Programming and Scripting

unknown test operator

hi all, i am using the below command in my script if ; then This statement is causing the problme "ScriptName.ksh: XXX-XXX: unknown test operator" could you please suggest me , how can i avoid these messages. Singhal (7 Replies)
Discussion started by: singhald
7 Replies

8. Shell Programming and Scripting

TEST operator help

Hi I want to group like this but syntactic is not right ... Thanks if Like this below does not work properly .. if then : else usage exit 1 fi (5 Replies)
Discussion started by: zam
5 Replies

9. UNIX for Advanced & Expert Users

unknown test operator

Hi, I have the following shell script : Nbr_BD_Link=0 Nbr_BD_Link=` sqlplus sysadm/${PSWD}@${DB_Name} << EOF | tail -4 | head -1 2>/dev/null set head off feedback off ; select count(*) from dba_db_links ; exit ; EOF ` echo ${Nbr_BD_Link} if ; then ... (4 Replies)
Discussion started by: big123456
4 Replies

10. Shell Programming and Scripting

:0: unknown test operator

I have gotten the script up to this point and it works fine a system but when i copy it to another unix server running solaris 9 as the original one , its gives an error './mon_fs.sh: 0: unknown test operator' . (see script bellow) can the Gurus see to this as i am just a beginner with... (2 Replies)
Discussion started by: ibroxy
2 Replies
Login or Register to Ask a Question