LinuxQuestions.org
Review your favorite Linux distribution.
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 12-24-2010, 01:26 AM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
shell script


hi guys i have a big problem i want to write shell script that give back argument is file or directory

how can i do it
e.q

./dfsh asd

give back its file

or

./dfsh /home/asd

give back its directory

in other word if argument contain "/" echo it's directory
regardless of it's exist or not

Last edited by mmhs; 12-24-2010 at 01:28 AM.
 
Old 12-24-2010, 01:31 AM   #2
avingard
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
man bash contains some useful information on boolean operators for files.

Here's what you need, with some comments:
Code:
#! /bin/bash

# command line parameters are accessed via $1, $2, etc.
if [[ -f $1 ]]; then   # the -f returns true if the parameter is a file, false otherwise
       echo "$1 is a file"
fi

if [[ -d $1 ]]; then    # the -d returns true if the parameter is a directory
      echo "$1 is a directory"
fi
Edit: I misunderstood the question; however, I'm leaving the answer as is since it is useful for what I assume is the ultimate purpose of your script. If you just need to distinguish between files and directories, using -f and -d are a better way to go about it than looking for '/'.

Last edited by avingard; 12-25-2010 at 01:29 AM.
 
Old 12-24-2010, 01:40 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You mean just to check if the variable passed contains "/" or not?

You can use the operator =~ :

Code:
#!/bin/bash

if [[ "$1" =~ "/" ]]
then
    echo "YES!"
else
    echo "NO!"
The $1 is the first command-line variable passed, and the =~ tests for regular expressions

Hope this helps,

Last edited by Snark1994; 12-24-2010 at 01:41 AM. Reason: Bad CODE tags
 
1 members found this post helpful.
Old 12-24-2010, 01:47 AM   #4
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Snark1994 View Post
You mean just to check if the variable passed contains "/" or not?

You can use the operator =~ :

Code:
#!/bin/bash

if [[ "$1" =~ "/" ]]
then
    echo "YES!"
else
    echo "NO!"
The $1 is the first command-line variable passed, and the =~ tests for regular expressions

Hope this helps,
thx man nice work it's better from my work i do it with:

echo $1 | grep "/" 1>/dev/null
if [ $? = 0 ]
then
echo "Yes"
else
echo "No"
fi

but your work is must bettre
 
Old 12-24-2010, 08:00 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Just as long as you realise that this in no way indicates if it is a file or directory???

The OPs own example is flawed if we assume that asd is in the home directory:
Quote:
./dfsh asd

give back its file

or

./dfsh /home/asd

give back its directory
If the assumption above is correct then this cannot be both a directory and a file and no amount of testing for slashes (/) will give you any information about its type,
only about its location.

avingard's examples are more correct
 
Old 12-24-2010, 08:09 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by mmhs View Post
echo $1 | grep "/" 1>/dev/null
if [ $? = 0 ]
then
echo "Yes"
else
echo "No"
fi
You can condense this into one line:
Code:
echo $1 | grep -q \/ && echo Yes || echo No
Also note that the if/then statement accepts a command as argument and check the exit status of the command itself, so that you can simply use:
Code:
if echo $1 | grep -q \/
then
  blah blah blah
The -q option of grep is very useful in this case.
 
Old 12-24-2010, 08:16 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by grail View Post
If the assumption above is correct then this cannot be both a directory and a file and no amount of testing for slashes (/) will give you any information about its type, only about its location.
I was aware of that, but the OP asked specifically for "in other word if argument contain "/" echo it's directory regardless of it's exist or not" - I guess we'll just have to wait upon clarification of the purpose of the script...
 
Old 12-24-2010, 10:10 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Thanks Snark ... missed that bit
 
  


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 01:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM

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

All times are GMT -5. The time now is 02:07 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration