Problem with -s option in IF


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with -s option in IF
# 8  
Old 11-05-2013
One thing you dont say: the shell you use and OS version... HP-UX used by default ksh I dont have anymore HP-UX 11.23 and above so cannot confirm all cases but till 11.23 ksh was default with posix shell... on 11.11 ksh is NOT ksh93 compliant ( Is a ksh88...)
You seem to mix diverse shell syntax this if can work sometime is sure to give you great issues: Once a shell chosen stick to its syntax, dont mix!
# 9  
Old 11-05-2013
Quote:
Originally Posted by HIMANSHU SOOD
Its pointing to same line that i have asked before .
Your previous code doesn't have -a at all, so it's obviously not exactly the same.


test <expression> and [ <expression> ] are equivalent. If <expression> has more than 1 clause then you need to use -a if you want to AND them within the same test.

[[ <expression> ]] is a Korn/bash enhancement which allows some extra operators like && for logical AND.

Quote:
Code:
if test -f ${MGCA_RESULTS_FILE} && test -f ${MGCA_TMP_LOG}

What you're doing here is using the shell's logical AND rather than the test command's logical AND. Which is fine, but it's probably helpful to understand the difference.
# 10  
Old 11-06-2013
Thanks vbe.Its all about working with different companies , so have to change my flavor of UNIX as well.

---------- Post updated 11-06-13 at 12:54 AM ---------- Previous update was 11-05-13 at 10:23 PM ----------

Carlo ,

Thanks for ur comment and good explanation .

When u say test <expression> and [ <expression> ] are equivalent , then why it does not work for ?

I have tried all possible ways but only above with test command and shell's logical AND worked fine.
Even test command with test command's logical AND also didnt work.

Looks bit wired but true Smilie

Thanks,
Himanshu Sood
# 11  
Old 11-06-2013
Quote:
Originally Posted by HIMANSHU SOOD
I have tried all possible ways but only above with test command and shell's logical AND worked fine.
Even test command with test command's logical AND also didnt work.
Without seeing your code, it's impossible to say.
# 12  
Old 11-06-2013
I would try this:
Code:
if [ -s "${MGCA_RESULTS_PATH:-nofile}" -a -s "${MGCA_TMP_LOG:-nofile}" ]

If the variables are null or unset the string "nofile" will be substituted instead. The quotes are there to wrap up any spaces, carriage returns, linefeeds or other characters special to the shell which may be causing your problems.

Andrew
# 13  
Old 11-06-2013
As I already told you it depends of your shell and the syntax, you are confusing 2 syntaxes!

Code:
  
if [  -s $1 -a  -s $2  ] # or
if [  -s ${1} -a  -s ${2} ]  # are identical to or if you prefer:
if [ -s ${MGCA_RESULTS_PATH} -a -s ${MGCA_TMP_LOG} ] # OR
if [ -s  "$MGCA_RESULTS_PATH" -a -s  "$MGCA_TMP_LOG" ]  # OR
if [ -s $MGCA_RESULTS_PATH -a -s $MGCA_TMP_LOG ]     # are identical to
if test -f ${MGCA_RESULTS_FILE} && test -f ${MGCA_TMP_LOG}
# NOT!
if [ -s ${MGCA_RESULTS_PATH} && -s ${MGCA_TMP_LOG} ]

 test "$1" -a "$2"   # should be written as:
    test "$1" && test "$2"

see:
Quote:
APPLICATION USAGE

The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:

test "$1" -a "$2"

should be written as:

test "$1" && test "$2"

to avoid problems if a user supplied values such as $1 set to '!' and $2 set to the null string. That is, in cases where maximal portability is of concern, replace:

test expr1 -a expr2

with:

test expr1 && test expr2

and replace:

test expr1 -o expr2

with:

test expr1 || test expr2

but note that, in test, -a has higher precedence than -o while "&&" and "||" have equal precedence in the shell.

Parentheses or braces can be used in the shell command language to effect grouping.

Parentheses must be escaped when using sh; for example:

test \( expr1 -a expr2 \) -o expr3

This command is not always portable even on XSI-conformant systems depending on the expressions specified by expr1, expr2, and expr3. The following form can be used instead:

( test expr1 && test expr2 ) || test expr3

The two commands:

test "$1"
test ! "$1"

could not be used reliably on some historical systems. Unexpected results would occur if such a string expression were used and $1 expanded to '!', '(', or a known unary primary. Better constructs are:

test -n "$1"
test -z "$1"

respectively.

Historical systems have also been unreliable given the common construct:

test "$response" = "expected string"

One of the following is a more reliable form:

test "X$response" = "Xexpected string"
test "expected string" = "$response"
You should read all of it:
http://pubs.opengroup.org/onlinepubs...#tag_20_128_18
This User Gave Thanks to vbe For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Is "stat: illegal option -- -" an issue with hyphenated filename or flag problem?

Hi. I'm trying to install VMWare Workstation to run a virtual machine on my Mac OS, but running the bundle from bash(xterm) sh VMware-workstation-Full-11.0.0-2305329.x86_64.bundle (as suggested in install guide) comes up with error:stat: illegal option -- - usage: stat Digging... (5 Replies)
Discussion started by: defeated
5 Replies

2. Shell Programming and Scripting

Problem in Using fgrep Command with pattern file option

Hi, i am using fgrep command with following syntax fgrep -v -f pattern_file_name file file contains few line and have the pattern which i am giving in pattern file. My Problem is : its is not giving any output. while i am using fgrep -f pattern_file_name file it is showing all... (4 Replies)
Discussion started by: emresearch
4 Replies

3. Shell Programming and Scripting

Problem with -c option in Mailx command

Hi, I am using mailx command in shell script. When i put -c option for CC , i get error message as "Unknown flag: -c" . I checked manual of mailx command and found that ~c to be used but after using ~c also it is not working. My shell is Korn. Kindly let me know what i have to do . Thanks (9 Replies)
Discussion started by: krishna_gnv
9 Replies

4. IP Networking

Problem when I Open my WireShark - No Interfaces Option available

Hi gurus of unix, I haved installed my openSolaris in a HP530, Recently I use the following command beadm destry opensolaris1 beadm destry opensolaris2 After That I reboot my Laptop. Well when I go to my work I try to open my wireshark tool, but the option in GUI interface does not appear!!!... (2 Replies)
Discussion started by: andresguillen
2 Replies

5. UNIX for Advanced & Expert Users

Problem with useradd, -p option in Solaris 10

Good day all. I'm trying to add a user with useradd and the -p option to assign a project name, but the result is that the user is created with an error message: "UX: useradd: user.root name should be all lower case or numeric." The command: useradd -d /export/home/tester -g rtpgrp -G... (2 Replies)
Discussion started by: BRH
2 Replies

6. Shell Programming and Scripting

Problem in file usage option

Hi, I have a dout in -u option in ls command. If we give ls -lt command it will dispaly the last modfied time. If we give ls -lu command it will display the last usage time of the file. My doubt here is Please refer the below commands. /appldata/rsc/dm/RejectData$ls -lt... (6 Replies)
Discussion started by: puni
6 Replies

7. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

8. UNIX for Advanced & Expert Users

Problem with Carbon copy (CC) option in mailx command

Hi, I have problems with the cc option in mailx command. Just went through some of the similar threads but none provides a satisfactory explanation. I have a script using the mailx command in the following way: (echo `cat mailsub.txt` ; uuencode attachment.csv attachment.csv) | mailx -s... (2 Replies)
Discussion started by: SmithaN
2 Replies

9. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. UNIX for Dummies Questions & Answers

problem in ssh with -i option

Hi All, I want to ssh to a remote terminal i did the following steps 1. On the client run the following commands: 2. $ mkdir -p $HOME/.ssh 3. $ chmod 0700 $HOME/.ssh 4. $ ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -P '' and then tried the following $ ssh -i $HOME/.ssh/id_dsa server ... (1 Reply)
Discussion started by: gauri
1 Replies
Login or Register to Ask a Question