LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-852154/)

mmhs 12-24-2010 12:26 AM

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

avingard 12-24-2010 12:31 AM

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 '/'.

Snark1994 12-24-2010 12:40 AM

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,

mmhs 12-24-2010 12:47 AM

Quote:

Originally Posted by Snark1994 (Post 4201796)
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

grail 12-24-2010 07:00 AM

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

colucix 12-24-2010 07:09 AM

Quote:

Originally Posted by mmhs (Post 4201801)
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.

Snark1994 12-24-2010 07:16 AM

Quote:

Originally Posted by grail (Post 4202046)
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...

grail 12-24-2010 09:10 AM

Thanks Snark ... missed that bit :(


All times are GMT -5. The time now is 08:40 AM.