You are not logged in.

#1 2022-08-09 19:34:23

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

I'm writing a simple program to run a script within a chroot however:

The following if statement checks if the file exists!

# script checks
if [[ -d $1 ]]
then
    echo "Script $1 exists!"
else
    echo "Script $1 does not exist!"
fi

However, regardless if the file exists. It says it exists, no matter what. I've always found variable and file checks in if statements to rarely work.


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#2 2022-08-09 19:46:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: I'm writing a simple program to run a script within a chroot however:

First -d tests whether the file exists and is a directory; is that what you really want?

Second, I'd wager properly quoting around $1 will solve your perceived problem.  To see why run the following:

if [[ -d ]]; then
   echo first
fi
if [[ -d "" ]]; then
   echo second
fi

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB