LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2019, 08:31 PM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Rep: Reputation: Disabled
Question if then else twice and if not found then run a command


Hello

I have this code:

Code:
if [[ $(find /path/to/folder1 -type f -not -path "*configs*" -size -800k 2>/dev/null) ]]; then
    echo "[[Warning]]: The files size is under 800 Kilobytes"
if [[ $(find /path/to/another/folder -type f \( ! -iname "123.file*" \) -not -path "*logs*" -size -40k 2>/dev/null) ]]; then
    echo "[[Warning]]: The file size is under 40 Kilobytes"
fi
else

Run a command here

fi

The target is:

Get an echo if a file under size found in any of the above paths or get both echo if a file under size found on both paths and run the command at the end only if not files under size found at any of the above two paths.

Both checks are tested and working but it seems that i have the if or else statements in wrong order ?

Thank you !

Last edited by bmxakias; 11-19-2019 at 09:22 PM.
 
Old 11-19-2019, 09:50 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
find is not great for such tests

it will exit 0 if it finds a file or not
it will only exit non-zero if something went "wrong" ( like incorrect arguments )



I'm not 100% certain I have the logic right

both have to find things to not run the command


Code:
#!/bin/bash

error_in_find () {
echo "something went very wrong with find" 1>&2
exit 1
}


_find1 () {
find /path/to/folder1 \
    -type f \
  ! -path "*configs*" \
    -size -800k \
    -printf "[[Warning]]: %f size is under 800 Kilobytes\n" \
    || error_in_find
}
_find2 () {
find /path/to/another/folder \
    -type f \
    \( ! -iname "123.file*" \) \
  ! -path "*logs*" \
    -size -40k \
    -printf "[[Warning]]: %f size is under 40 Kilobytes\n" \
    || error_in_find
}

dontrun=y
grep --colour=no "." < <(_find1) || unset dontrun
grep --colour=no "." < <(_find2) || unset dontrun

[[ $dontrun ]] || run_your_command



#
testing to exit code of grep, "." is any char. so if the finds printed anything grep returns 0
if the finds don't print anything, grep returns 1

when grep finds something, we unset the dontrun var

when grep does not find something, we unset the dontrun var

so, both have to print ( find ) something to not run
if any of them fail to print ( find ) something the run_your_command is run.

Last edited by Firerat; 11-19-2019 at 10:05 PM.
 
Old 11-19-2019, 10:31 PM   #3
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Quote:
find is not great for such tests
I used that as it works with no issues when i use the two commands separate...


Quote:
both have to find things to not run the command
No. If found one or both the command should not run. The command will only run only if no small files found on both paths.
 
Old 11-19-2019, 11:26 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by bmxakias View Post
I used that as it works with no issues when i use the two commands separate...
ahh, yes you are right
$(..) is expanding
[[ ]] is only checking if null or not


Quote:
Originally Posted by bmxakias View Post
No. If found one or both the command should not run. The command will only run only if no small files found on both paths.
if either of the finds print, then do not run


Code:
#dontrun=y
grep --colour=no "." < <(_find1) && dontrun=y
grep --colour=no "." < <(_find2) && dontrun=y

[[ $dontrun ]] || run_your_command
# or
# [[   $dontrun == y ]] || run_your_command
# [[ ! $dontrun == y ]] && run_your_command
# [[   $dontrun != y ]] && run_your_command
#
#
or
Code:
run=y
grep --colour=no "." < <(_find1) && run=n
grep --colour=no "." < <(_find2) && run=n

[[ $run == n ]] && run_your_command

[edit]
this is wrong

Code:
unset count
if [[ $(find /path/to/folder1 -type f -not -path "*configs*" -size -800k 2>/dev/null) ]]
then
    echo "[[Warning]]: The files size is under 800 Kilobytes"
    ((count++))
fi
if [[ $(find /path/to/another/folder -type f \( ! -iname "123.file*" \) -not -path "*logs*" -size -40k 2>/dev/null) ]]
then
    echo "[[Warning]]: The file size is under 40 Kilobytes"
    ((count++))
fi

if [[ $count -lt 2 ]]
then
    run_command
fi


#


Code:
unset count
if [[ $(find /path/to/folder1 -type f -not -path "*configs*" -size -800k 2>/dev/null) ]]
then
    echo "[[Warning]]: The files size is under 800 Kilobytes"
else
    ((count++))
fi
if [[ $(find /path/to/another/folder -type f \( ! -iname "123.file*" \) -not -path "*logs*" -size -40k 2>/dev/null) ]]
then
    echo "[[Warning]]: The file size is under 40 Kilobytes"
else
    ((count++))
fi

if [[ $count == 2 ]]
then
    run_command
fi


#

Last edited by Firerat; 11-19-2019 at 11:32 PM.
 
1 members found this post helpful.
Old 11-20-2019, 12:36 AM   #5
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Works great !!!

Thank you
 
Old 11-20-2019, 04:41 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Alternatively you can have result variables:
Code:
res1=$(find /path/to/folder1 -name "*configs*" -prune -o -type f -size -800k -print 2>/dev/null)
res2=$(find /path/to/another/folder -name "*logs*" -prune -o -type \! -iname "123.file*" -size -40k -print 2>/dev/null)
if [[ -n $res1 ]]; then
  echo "[[Warning]]: The files size is under 800 Kilobytes ($res1)"
fi
if [[ -n $res2 ]]; then
  echo "[[Warning]]: The file size is under 40 Kilobytes ($res2)"
fi
if [[ -z $res1$res2 ]]; then
  : run command
fi
The finds in this example really prune the unwanted pathes, that's faster.

Last edited by MadeInGermany; 11-20-2019 at 04:45 AM.
 
1 members found this post helpful.
Old 11-20-2019, 06:39 AM   #7
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
I will check it. Thank you

Is it good to use the 2>/dev/null or not needed at all ?
 
Old 11-20-2019, 06:58 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
The 2>/dev/null can suppress a "permission denied". Normally not needed if run as root or the owner of the files/folders.

BTW in the case of -prune you need the explicit -print in order to not implicitly print the pruned folders.
 
1 members found this post helpful.
Old 11-20-2019, 07:16 AM   #9
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Ok thank you !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] No package 'x11' found No package 'xext' found No package 'xdamage' found No package 'xfixes' found No package 'x11-xcb' found Jigsaw Linux From Scratch 14 02-23-2021 08:35 PM
[SOLVED] Is there a cli method of running a command twice without having to type it twice? powderburns Linux - Software 4 03-24-2017 09:55 AM
if file *.txt then do x else if *.mp3 file then do xx (bash) Techno Guy Linux - Newbie 3 04-12-2009 08:52 PM
Yum runs once or twice within a small timeframe and then will not run until a reboot. KTheorem Linux - Software 1 11-27-2006 05:53 AM
Prolog Help with if-then-else-else if?? magicvash Programming 1 05-27-2005 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration