Is there an easy way of checking for the existence of a file that ends with the extension .order and if it exists do something? if not do nothing
Hello,
Could you please use the following command. This is just an example you can put this code in a script with .ksh extension and run it.
$ filename="something.order"
$ if [[ -e $filenwme ]]
> then
> echo "R. Singh"
> else
> echo "nothing"
> fi
As above code shows if file exist it will show R. Singh if doesn't then it will show nothing. Just an example for you.
Note: this I have ran as command line not as a script.
Thanks,
R. Singh
hi thanks for the reply,
I need to find all files with the extension .order, so I tried the following:
#!/bin/bash
filename="*.order"
if [[ -e $filename ]]
then
echo "yes its there"
else
echo "no its not"
fi
it keeps returning its not there when the file is present.
Thanks
Paul
try something like:
if [ -n "$(ls | grep "[.]order$")" ]
then
echo found
else
echo not found
fi
Something similar...
if [ -n "$(ls *.bin 2>/dev/null)" ];then
echo "Okk..."
else
echo "Oops"
fi
--ahamed
#!/bin/bash
files=( *.order )
if (( "${#files[@]}" > 0 )); then
...
else
...
fi
for file in "${files[@]}"; do
something with $file
done
exists () {
[ -e "$1" ]
}
filename=*.order
if exists $filename; then
echo yes
fi
--
With the external program ls
:
filename=*.order
if ls $filename >/dev/null 2>&1; then
echo hello
fi
A more efficient approach.
if grep -q '\.order$'; then
echo match
else
echo no match
fi
It's more efficient in several ways.
First, fewer subshells need to be forked.
Second, since grep's output isn't required, there's no need to pipe data from grep to a command substitution parent shell.
Third, suppose that there are very many matches in the ls output. We only need to find the first one. Once a single match is found, with -q, grep can exit immediately since the result is known to be true.
On an unrelated note, unless you intend to allow some sort of expansion between them, or unless you are trying to avoid a collision with another set of quotes, it's a good habit to use single quotes instead of double quotes. Also, while a trivial difference, it's also more efficient to parse single-quoted text.
Regards,
Alister
---------- Post updated at 03:43 PM ---------- Previous update was at 03:39 PM ----------
You can probably also redirect stdout to /dev/null and test the exit status directly.
if ls *.bin >/dev/null 2>&1; then
Regards,
Alister
---------- Post updated at 03:46 PM ---------- Previous update was at 03:43 PM ----------
With the usual shell defaults, the if-condition will always succeed, since if nothing matches the pattern the pattern remains unchanged and is assigned as the sole member of the array. For this code to work, nullglob
must be enabled.
Regards,
Alister
---------- Post updated at 03:59 PM ---------- Previous update was at 03:46 PM ----------
In spirit, the following is akin to Scrutinizer's approach, but instead of using a function parameter to isolate the first of potentially many matches, it uses the for-loop's list assignment and an unconditional break.
for f in *.order; do
break
done
if [ -e "$f" ]; then
echo yes
fi
Regards,
Alister