Variable on If Statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable on If Statement
# 1  
Old 12-20-2012
Variable on If Statement

Hi,

I am tasked to modify soem script and I come accross a line which I dont fully understand. I tried searching online but I couldnt get a good explanation on it.

Here it the part of the code:

Code:
 
PAY_RT=`cat $TEMPFILE | cut -f2 -d","`
 
if [ ${PAY_RT:-BLANK = 'P' ];
then
     PAY_RT=R
fi

What is the colon (:) and hypen (-) means on the if statement? I never used this on any of my if statement before.

There are also variation of :
Code:
if [ ${PAY_AMNT:-"00" = "000" ];
then
     PAY_AMNT=00
fi

# 2  
Old 12-20-2012
It is called "parameter expansion".

An extract from the Korn shell man page:

Quote:
${parameter:-word}
If parameter is set and is non-null then substitute its value; otherwise substitute word.
Although your code seems to be missing the closing }.
# 3  
Old 12-20-2012
Anything of the form ${...}is a bash parameter substitution, the one you are seeing should read ${PAY_AMNT:-"00"} and could be read substitute this marker with the value of PAY_AMNT or if PAY_AMNT is null use "00"
# 4  
Old 12-20-2012
That really helps! Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable IF statement in awk

I have multiple requirements to run data extracts (with the same basic requriement / resulsts) however the parameters of the extracts change with each request. To help make life easier I am writing a script where the parameters for the specific request are set at the start and then used as needed.... (6 Replies)
Discussion started by: meike
6 Replies

2. Shell Programming and Scripting

awk - updating variable in if statement

I have another question I am stuck at :wall: I have a text file with two columns, like so... 2 0.0627279 3 0.0794451 4 0.108705 5 0.137739 6 0.190394 7 0.217407 8 0.241764 9 0.344458 10 0.460762 I'd like to go through the file line by line until the value in the second column... (3 Replies)
Discussion started by: origamisven
3 Replies

3. Shell Programming and Scripting

set variable using 'ls' with an 'or' conditional statement

Hi, I would like to set a variable using ls, but I need to be able to list two possibilities simultaneously, i.e., I'd like to do this all on one line. These are the two possible directories, but keep in mind only *one* will be present at any given time: drwxrwxrwx 4 qtv qtv 16384 Nov 9... (5 Replies)
Discussion started by: goodbenito
5 Replies

4. Programming

Assign variable for INSERT INTO statement

Hello, Can anyone tell me that, How can I assign variable to shell script variable, which i need to use in INSERT INTO statement? my shell script variables are, EMPNAME=`regular expression` EMPID=`regular expression` EMPBDATE=`regular expression` Now through ksh script I am... (16 Replies)
Discussion started by: Poonamol
16 Replies

5. Shell Programming and Scripting

Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results LINE='device for 0101a01: lpd://172.25.41.111:515' prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7` echo $prt My if statement to select just what i want.. IFS=$":" while read prt... (11 Replies)
Discussion started by: ggoliath
11 Replies

6. Shell Programming and Scripting

Assigning Variable to AWK statement

Hi, The following command runs on in the Korn shell prompt. however i want to output the value of this to a variable. Can anyone provide a solution? echo 'ABC,DEF,"G,HI,J",KLM,"MNi,O"'| awk -F "\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("\,","~^~",$i)}}1' (2 Replies)
Discussion started by: ladarlsan
2 Replies

7. Shell Programming and Scripting

If statement in awk with external variable

So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have. # !/bin/sh echo "Enter client ID" read ID echo "Enter month (01, 02, 03)" read month echo "Enter day (03, 15)" read day echo "Enter Year (07, 08)" read... (6 Replies)
Discussion started by: doublejz
6 Replies

8. Shell Programming and Scripting

help with variable substitution in case statement

I have a script where I take input from user and see if it falls in list of valid entry. --------------- #!/bin/ksh VALID_ENTRIES="4|5|6" echo "enter your choice" read reply IFS="|" echo "valid entries are $VALID_ENTRIES" case "$REPLY" in Q|q ) IFS="$IFS_SAVE";return ;;... (5 Replies)
Discussion started by: bhav_shah82
5 Replies

9. Shell Programming and Scripting

Can't interpret variable in if statement

Can someone help me out here. I can't get this piece of code to work. i.e. $ALL_EVENTS does not get interpreted in the if brackets. The first part is the code, the second part is the execution of the code. Note: $ALL_EVENTS does equal 2, but there is no value once passed to the if statement. ... (4 Replies)
Discussion started by: jwholey
4 Replies

10. Shell Programming and Scripting

Using variable in case statement

I want to do this: Ex 1: case $answer in 1|2|3|4|5) echo $answer;; x) break;; *) echo "Invalid selection. Try again.";; esac But I need the part "1|2|3|4|5" to be fetched from a variable, like so: Ex 2: case $answer in $cases) echo $answer;; x) break;; *) echo "Invalid... (2 Replies)
Discussion started by: fialia
2 Replies
Login or Register to Ask a Question