LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2017, 01:55 AM   #1
ducati94
LQ Newbie
 
Registered: Jul 2017
Posts: 2

Rep: Reputation: Disabled
Check Folder/Directory and Echo - super newbie :)


Hi there

newbie here, i am trying to get the following script to work, but for the life of me, i cannot get it working.

Note: the <results> </results> are required. This is for JSS Extension Attribute.

Code:
#!/bin/bash
 
DIR=/Desktop/OneDrive - Name of Company
 
if [ ! -d $DIR ]; then
echo <results> "Directory exists" </results>
else
echo <results> "Incorrect" </results>
fi

i have success in doing something similar for Applications but not folder. Below is just an example of what i am trying to achieve

Code:
#!/bin/sh
# Identify App Store vs non App Store.

if [ -e /Applications/Numbers.app ]; then
    if [ -e /Applications/Numbers.app/Contents/_MASReceipt/ ]; then
        echo "<result>Apple</result>"
    else
        echo "<result>BCGS</result>"
    fi
else
    echo "<result>Not Installed</result>"
fi
And yes i have tried playing around with where the <result> tag is put as well. Where the result tag is in " "

Thanks in advance please be gentle lol

Last edited by ducati94; 07-26-2017 at 02:02 AM.
 
Old 07-26-2017, 02:40 AM   #2
aus9
LQ 5k Club
 
Registered: Oct 2003
Location: Australia
Distribution: IceWM on Debian
Posts: 5,483

Rep: Reputation: Disabled
firstly I am not a real coder but me thinks your contrapositive statement needs to be reversed

here is test
Code:
./script                                                                                                                                                                       
Directory not found                                                                                                                                                                          

mkdir Desktop/OneDrive                                                                                                                                                         
gordon@neo:~$ ./script                                                                                                                                                         
Directory exists
script changed as I don't use JSS...to
#!/bin/bash

DIR=~/Desktop/OneDrive

if [ -d $DIR ] ; then echo "Directory exists"
else
echo "Directory not found"
fi
 
Old 07-26-2017, 02:53 AM   #3
ducati94
LQ Newbie
 
Registered: Jul 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by aus9 View Post
firstly I am not a real coder but me thinks your contrapositive statement needs to be reversed

here is test
Code:
./script                                                                                                                                                                       
Directory not found                                                                                                                                                                          

mkdir Desktop/OneDrive                                                                                                                                                         
gordon@neo:~$ ./script                                                                                                                                                         
Directory exists
script changed as I don't use JSS...to
#!/bin/bash

DIR=~/Desktop/OneDrive

if [ -d $DIR ] ; then echo "Directory exists"
else
echo "Directory not found"
fi
thanks for the reply

does it matter if it has spaces ? because my structure would look like OneDrive - Name Of Company
the above inclused - as well.
 
Old 07-26-2017, 03:29 AM   #4
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 553

Rep: Reputation: 277Reputation: 277Reputation: 277
Quote:
Originally Posted by ducati94 View Post
does it matter if it has spaces ?
Yes, it matters.

So I suggest you do something like this:

Code:
#!/bin/bash
 
DIR="/Desktop/OneDrive - Name of Company"
 
if [ -d "$DIR" ]; then
  echo <results> "Directory exists" </results>
else
  echo <results> "Incorrect" </results>
fi
I removed the "!" as it will invert the results. "-d" returns True if $DIR exists and is a directory.
 
Old 07-26-2017, 03:35 AM   #5
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,852

Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
Try this:

Code:
#!/bin/bash

 DIR='OneDrive - Name of Company'

cd /Desktop
ls  | grep "$DIR" && echo "The folder is found" || echo "The folder cannot be found"
#Script ends
Got idea from this link: http://quickbytesstuff.blogspot.sg/2...er-exists.html

Good luck!

Last edited by JJJCR; 07-26-2017 at 03:35 AM. Reason: edit
 
Old 07-29-2017, 07:33 AM   #6
acescript
LQ Newbie
 
Registered: Jul 2017
Location: Lagos
Distribution: Red Hat
Posts: 12

Rep: Reputation: Disabled
It's not so clear what exactly you want to achieve because your script is doing contrary to what's obvious:

Quote:
#!/bin/bash

DIR=/Desktop/OneDrive - Name of Company

if [ ! -d $DIR ]; then
echo <results> "Directory exists" </results>
else
echo <results> "Incorrect" </results>
fi
Question 1: Is /Desktop/OneDrive a directory or a file, if its a directory the expression in the test

Quote:
! -d $DIR
will execute the first echo statement if the $DIR doesn't exists. These contradict themselves. The correct code should be:

Quote:
#!/bin/bash

DIR=/Desktop/OneDrive - Name of Company

if [ -d $DIR ]; then
echo <results> "Directory exists" </results>
else
echo <results> "Incorrect" </results>
fi

For the second code

Quote:

#!/bin/sh
# Identify App Store vs non App Store.

if [ -e /Applications/Numbers.app ]; then
if [ -e /Applications/Numbers.app/Contents/_MASReceipt/ ]; then
echo "<result>Apple</result>"
else
echo "<result>BCGS</result>"
fi
else
echo "<result>Not Installed</result>"
fi
The
Quote:
-e /Applications/Numbers.app
appears to be a file. If this is true it contradicts this line

Quote:
-e /Applications/Numbers.app/Contents/_MASReceipt/
because you cannot make a sub-directory of a file.

Always use the appropriate syntax when writing your code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Is it possible to deny super user to access a file/folder in linux. karanace Linux - Security 12 03-03-2012 12:22 AM
How do you echo the folder name your currently in (using SHELL)? Techno Guy Linux - Newbie 8 03-09-2009 02:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 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