What's wrong with this line: if ${TEST:?} ; then echo empty; fi


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What's wrong with this line: if ${TEST:?} ; then echo empty; fi
# 1  
Old 02-23-2008
What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty.

TEST=
if ${TEST:?} ; then echo empty; fi

But it doesn't work. What's wrong?
# 2  
Old 02-23-2008
Code:
$ cat > foo.ksh
#!/bin/ksh
TEST=
FOO=${TEST:?empty}
$ chmod +x ./foo.ksh$ ./foo.ksh
./foo.ksh: line 3: TEST: empty
$ sed 's/^TEST=/TEST=something/' foo.ksh > foo2.ksh
$ chmod +x ./foo2.ksh
$ ./foo2.ksh
$

No need for the conditional structure, a simple variable assignment will do it.

Cheers,
ZB
# 3  
Old 02-23-2008
${TEST:?} will cause your script to exit immediately. If you want to exit more
gracefully or do some cleanup, the following method of testing for an null or
empty string may be more useful.

Code:
#!/usr/bin/ksh

TEST=

if [[ ! $TEST ]]
then
   echo "empty"
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. UNIX for Dummies Questions & Answers

What is wrong with: echo $PATH | sed s/:/\\n/g

Hello all! I am on Mac (10.8.4) and my shell tcsh (man says version: Astron 6.17.00). Just to precise my tcsh: echo $LC_CTYPE UTF-8 I want to replace all ':' with a new line, to get all paths on one line. I don't find a way to make my shell accept the "\n" My start was: echo... (17 Replies)
Discussion started by: marek
17 Replies

4. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

5. Shell Programming and Scripting

How to echo message when file is empty.

If a file is empty then it must print the message "no data found" eg: if then echo "no data found" your help is really appreciated. (3 Replies)
Discussion started by: javeedkaleem
3 Replies

6. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

7. Shell Programming and Scripting

howto add line as a first line into a non empty file

Hi Trying to do like this : echo "$variable1\n $(cat file.txt)" but it only adds one time. When I run this cmd again with different variable it only replaces line of variable1. How to add constantly line into first line in file ? (3 Replies)
Discussion started by: presul
3 Replies

8. UNIX for Dummies Questions & Answers

echo is returning empty

OS - AIX 6.1 I am have below entry in my .profile DS_LIB=/opt/IBM/InformationServer/Server/DSEngine/bin export DS_BIN When I do echo $DS_BIN, echo is returning empty $ echo $DS_BIN $ I never had this kind of issue earlier, please help me. Thanks srimitta (4 Replies)
Discussion started by: srimitta
4 Replies

9. Shell Programming and Scripting

test file empty

hi, I test number on a file, but I have a problem when my file is empty, because the test isn't realized , do you have a idea to realize treatment when file is empty my test script shell VALUE=5000 for i in `cat /tmp/list | awk ' FS="|" { print $2 } '` do if then ... (13 Replies)
Discussion started by: francis_tom
13 Replies

10. Shell Programming and Scripting

if test for empty and non-existing file

How to write this condition in ksh? if myfile is empty or myfile does not exist then do action1 fi is this OK? if ] -o ] then then do action1 fi Thanks. (3 Replies)
Discussion started by: GNMIKE
3 Replies
Login or Register to Ask a Question