Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the type of command line arguments 6

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hello,

I have another question regarding how I could check each of the arguments passed to a script to determine if they are a regular file. As you can probably guess I don't have much experience working with Unix shell scripts. As of now I'm thinking I should do something like

Code:
for ARGUMENT in $* ; do
        //somehow check to see if $ARGUMENT is a regular file 
        //(i.e.  a text or executable file
        echo $ARGUMENT
done

The details on how to check each argument to see if they satisfy the criteria is where where I'm lost.

Any help is appreciated.
 
Code:
for ARGUMENT in $* ; do
        if [ -f $ARGUMENT ] ; then 
              echo $ARGUMENT
        endif
done

you can also use

-a file true if the file exists (Korn/Bash)
-b file true if the file is a block special device
-c file true if the file is a character special device
-d file true if the file is a directory
-f file true if the file is a regular file
-g file true if the file has the SGID permission bit set
-G file true if the file’s group matches the user’s group
-k file true if the file has the sticky bit set
-L file true if the file is a symbolic link
-O file true if the user running this command owns this file (Korn/Bash)
-p file true if the file is a named pipe or fifo
-r file true if the file is readable
-s file true if the file has a size greater than zero
-S file true if the file is a socket
-t filedes true if file descriptor is associated with a terminal device
-u file true if the file has its SUID permission bit set
-w file true if the file is writable
-x file true if the file is executable
 
Hi LunaPan,

I tried your suggestion. Here is my code

Code:
for ARGUMENT in $* ; do
        if[ -f $ARGUMENT ] ; then
                ls -l $ARGUMENT
        else
                echo "$ARGUMENT is not a regular file or else does not exist"
        fi
done

But, this is the output I'm getting.

./labscript: line 23: syntax error near unexpected token `then'
./labscript: line 23: ` if[ -f $ARGUMENT ] ; then'


Can anyone tell me what I'm doing wrong?

Thanks!
 
Replace this:
if[
by this:
if[highlight] [/highlight][

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Well that worked. Thanks a lot for both of your help. It seems shell scripting can be really finicky.
 
In shell, tokens are separated by whitespace. The way you had written it at first, the token it was trying to look up was [tt]if[[/tt]. That's not a command it knows about.

The [tt][[/tt] is a command in its own right; its real name is [tt]test[/tt], but [tt][[/tt] is an "alias" of sorts to make it look nicer (and may even be a shell builtin command). The [tt]if[/tt] builtin just takes a command and tests its return value. [tt][[/tt] is just the most commonly used command.

With that in mind, consider that instead of using the [tt][[/tt] command, I use, for example, the [tt]grep[/tt] command as the condition for [tt]if[/tt]. I say:
Code:
if grep -q something file; then
...
I couldn't have written:
Code:
ifgrep -q something file; then
...
because the parser needs to know where one token ends and the next begins.

Granted, a left bracket isn't something you'd normally expect to be part of a command name, but in this case, it is.


Also, I suggest you write:
Code:
for ARGUMENT in "$@" ; do
...
That is, change [tt]$*[/tt] to [tt]$@[/tt] and put it in quotes. That will ensure that your script operates correctly even if some of your arguments have whitespace in them.
 
I would add to chipperMDW suggestion. If you want to be able to deal with arguments with whitespace in them, you have to quote all usages of variables holding filename. Like this:
Code:
for ARGUMENT in "$@"; do
        if[ -f [COLOR=yellow green]"[/color]$ARGUMENT[COLOR=yellow green]"[/color] ] ; then
                ls -l [COLOR=yellow green]"[/color]$ARGUMENT[COLOR=yellow green]"[/color]
        ....

Tedious, isn' it ?
Is there a more simple solution ?

I got this problem recently with a free software installation script used under cygwyn on a PC. The home directory of each user was located under a directory with embedded spaces like this [tt]/cygdrive/d/Documents and settings/user[/tt] (ok it is an awfull name for a HOME directory) and the script was unable to create a subdirectory using:
Code:
mkdir -p $HOME/.software
I had to quote every usage of some variables [sad]

--------------------

Denis
 
Good point; thanks.


> Is there a more simple solution ?

Not that I know of, but there are ways to automate quoting the variables. For example, you could use a sed or Perl script, or create a text editor macro.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top