option in if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting option in if statement
# 1  
Old 09-23-2005
option in if statement

does anyone know what the -a in an if statement is referring to

ex...

if [ ! -a "$INLOG" ]
# 2  
Old 09-23-2005
man "test" will tell you. It tests whether the file in $INLOG variable exists.
# 3  
Old 09-23-2005
that's what I though, but...

that's what I figured. I'm trying to rationalize this syntax...

if [ not and file ] then....


I'm not comprehending the logic here, it's checking for ! (not) what. and checking for file. So where does it say to check the pwd for that file name. What value does ! by itself give back.
# 4  
Old 09-23-2005
It reads "if file does not exist"

Code:
a) -a $INLOG returns false
b) ! false is true

Therefore the test succeeds when the file does not exist.


Fun If Fact

Quote:
At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.

The most compact syntax of the if command is:

if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi

The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.

The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form /dev/fd/N, then file descriptor "N" is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests.
# 5  
Old 09-23-2005
Quote:
Originally Posted by cubs0729
if [ ! -a "$INLOG" ]
My "man test" does not have a "-a" option, but that may depend on my OS. The "man test" on my system says you can test this (and much more):

-e file exists
! -e file doesn't exist
-s file exists and has a size greater than zero
! -s file doesn't exist and/or has a size of zero

You can test many, many situations and this is very handy.

Sometimes you would like to know if a file exists, like so: if [ -e filename ] or if a file does not exist, like so: if [ ! -e filename ].
You could write an if-statement to check if a file exists and give an error if it doesn't, like so:
Code:
if [ -e filename ]; then
echo "It's here and I don't want to do anything, because I only want to know if it's NOT here"
else
echo "File's not here!"
fi

But checking for the "not-existance" is sometimes much more efficient, because you might not care about the other situation:
Code:
if [ ! -e filename ]; then
echo "ERROR! File's not here!"
fi

It all depends on the function of your if-statement.
# 6  
Old 09-23-2005
Yes, it does depend on your system. My Solaris system shows -a, my AIX does not but it works in both systems.
# 7  
Old 09-24-2005
Yes - and to confuse matters -a can also be used as a binary and operator, e.g.

if [ -a "${myfile}" -a -z "${somestring}" ]; then
...
fi

Take a peek at the Solaris 8 manual page...

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Unrecognized option: sparc-sun-Solaris2.10/bin/as: unrecognized option `-m32'

Hi, I installed some packages required by an app built with python. But when I try python setup.py install, I get the following error: /opt/csw/lib/gcc/sparc-sun-solaris2.10/5.2.0/../../../../sparc-sun-solaris2.10/bin/as: unrecognized option `-m32' Could anyone tell me what's wrong... (4 Replies)
Discussion started by: Kimkun
4 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

If option statement

I working on my Apache2 restore script. This is the code so far updatedb prompt_list "Whick configuration_backup file do you want to use?" `find /restore/configuration/ -name configuration_backup* -type d` BACKUP_OBJECT=${CHOICE} echo "--------------------------------------" cd... (10 Replies)
Discussion started by: dannyvdberg
10 Replies

4. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

5. 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

6. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

7. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

8. Programming

g++ and the -R option

hi everybody, can somebody tell me what -R option on g++ on solaris means : g++ -DAIX -fpic -static -o printps printps.o -L/epost2/blitz/xercesc1_1 /lib -L/oracle/OraHome/lib32/ L/epost2/blitz/lib -lxerces-c1_1 -lhmltods -lhmlt ops -lgeneric -lnotify -lutil -lclntsh `cat... (0 Replies)
Discussion started by: eternalflame
0 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. Programming

cc option

my yacc output file y.tab.c is not compiling using cc y.tab.c -ly command .possibily option flag -ly is not correct.i m using red hat linux 9.please give solutions. (4 Replies)
Discussion started by: kuldeep_bora
4 Replies
Login or Register to Ask a Question