3

I want to check if a directory exists on Android.

From an answer to Check if a file exists with a wildcard in a shell script, I get an idea. So I use ADB like below.

if [ adb shell ls ${test_dir}  2> /dev/null ] ;
then
   echo "files exist"
else
   echo "files do not exist"

fi

I am new to Bash scripting. I know adb shell ls will return all file names. But what's meaning of 2> /dev/null?

And I only care about files do not exist condition. So how do I invert the condition?

Second version

if [ ! adb shell ls ${test_dir}  2> /dev/null ] ;
then
   echo "files exist"
else
   echo "files do not exist"

fi

Adding ! cannot work for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CoXier
  • 2,523
  • 8
  • 33
  • 60

1 Answers1

1

In the expression:

if [ adb shell ls ${test_dir}  2> /dev/null ]

The redirection 2> /dev/null redirects any message on stderr (any error resulting from abd shell ls ${test_dir} to /dev/null quaintly known as the bit bucket. In essence the bit bucket is a system device node that goes nowhere. So you can copy or redirect any output you like to /dev/null and it simply goes away (meaning it isn't copied anywhere and isn't redirected any further -- it just goes into the bit bucket -- a black-hole for convenience) This has the effect of suppressing any output from the test itself.

The second part of your question asks how to negate (invert) a test clause. The simple answer is to put a '!' at the front of the test.

Let me know if you have further questions.


After further discussion and sorting out that adb is a android tool, the solution was to simply check the return following the execution of the command itself, e.g.

$ adb shell ls ${test_dir}  2> /dev/null

and then test the return with

if [ "$?" -ne '0' ]; then 
    # handle error
fi

If you have further questions on this issue, just let me know.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • `if [ ! adb shell ls ${test_dir} 2> /dev/null ] ` ? But it does not work for me. – CoXier Apr 22 '18 at 05:22
  • Let me look at your link further. `abd` isn't a bash command or test. What you want if you are testing for an empty directory is: `[ -z "$(ls -A "$test_dir")" ]` If you want to know if it has file, then `[ "$(ls -A "$test_dir" | wc -l)" -gt 0 ]`. – David C. Rankin Apr 22 '18 at 05:23
  • That's OK, my eyes are crossing as I get older `:)`. Were you testing for files in a directory, or for an empty dir? – David C. Rankin Apr 22 '18 at 05:26
  • I want to check dir exists. It's my fault and I will update my question. – CoXier Apr 22 '18 at 05:28
  • 1
    If you want to test that a directory exists, then use `if [ -d "$dirname" ]; then echo "$direname exists"; else echo "$dirname not found"; fi` You can also just check `if [ ! -d "$dirname" ]; then echo "$dirname doesn't exist"; fi` – David C. Rankin Apr 22 '18 at 05:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169523/discussion-between-coxier-and-david-c-rankin). – CoXier Apr 22 '18 at 05:30