The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Plink connections to multiple unix servers Moxy Shell Programming and Scripting 2 08-28-2008 10:42 PM
Accessing variables of one shell script in another shell script looza Shell Programming and Scripting 2 06-30-2008 08:13 PM
How we can use plink? shekhar_ssm Shell Programming and Scripting 0 03-16-2008 12:35 PM
How to pass a parameter from one Shell-script to another Shell-script subodhbansal Shell Programming and Scripting 2 09-22-2007 06:19 AM
Plink problem....only works if passwd is in the script Yinzer955i UNIX for Dummies Questions & Answers 3 08-29-2006 02:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-26-2008
praveen.1 praveen.1 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
Exclamation plink and shell script

This is my shell script... test.sh

DIRECTORY=/XYZ/PQR
if [ -f $DIRECTORY/TTT/* ]; then
echo "In test.."
else
echo "lno.."
fi


when i run this script through a putty its output is:
./test.sh: line 2: [: too many arguments
lno..


But when i run the same script using plink its running fine and its output is as expected...

wanted to know why this is happening...
  #2 (permalink)  
Old 08-26-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by praveen.1 View Post
This is my shell script... test.sh

DIRECTORY=/XYZ/PQR
if [ -f $DIRECTORY/TTT/* ]; then
echo "In test.."
else
echo "lno.."
fi


when i run this script through a putty its output is:
./test.sh: line 2: [: too many arguments
lno..

If the wildcard expands to more than one file, you will get that error.

Use a function instead:
Code:
is_file () 
{ 
    for f in "$@";
    do
        [ -f "$f" ] && return;
    done;
    return 1
}

if is_file "$DIRECTORY"/TTT/*; then
...
Quote:
But when i run the same script using plink its running fine and its output is as expected...

There there's something wrong with plink (whatever that is), or you are using it in a situation where the wildcard doesn't expand to more than one file.
  #3 (permalink)  
Old 09-08-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
There's no reason why a session invoked through putty should behave differently to one invoked through plink. That's very odd...

But as cfajohnson says, using [ -f * ] is generally something to avoid. What are you actually trying to test for?
  #4 (permalink)  
Old 09-08-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Smiling Dragon View Post
[ -f * ] is generally something to avoid.

Not generally: always.

Unless there is exactly one matching file, the result will always be wrong (a syntax error, or a false positive).
  #5 (permalink)  
Old 09-08-2008
praveen.1 praveen.1 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
plink and shell script

if i add the interperet explicitly then its working fine..

#!/bin/sh
DIRECTORY=/XYZ/PQR
if [ -f $DIRECTORY/TTT/* ]; then
echo "In test.."
else
echo "lno.."
fi


This works fine..are these shell dependent?i am using bash shell..
  #6 (permalink)  
Old 09-08-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by praveen.1 View Post
if i add the interperet explicitly then its working fine..

#!/bin/sh
DIRECTORY=/XYZ/PQR
if [ -f $DIRECTORY/TTT/* ]; then
echo "In test.."
else
echo "lno.."
fi


This works fine..

That will not work fine unless there is exactly one file that matches the pattern.
Quote:
are these shell dependent?i am using bash shell..

That is wrong in any and all shells.

Have you tried it on an empty directory?

Have you tried it on a directory containing more than one file?

If will also not be fine if $DIRECTORY contains whitespace.

See the is_file function I posted earlier.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0