What does : do in this instance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What does : do in this instance
# 1  
Old 05-07-2009
What does : do in this instance

Guys please see below functions to return a status depending on user input. Both seem to work the same.

The second way has a : line which i can't understand or see in a ksh manual anywhere. Instead of doing the variable change if its empty on this line the first function simply does it on the case line. They both work but which is best and what does the : do?

Code:
confirm_yes()
{
          messages="$1"
          while :
          do
              print "${messages} [yn,] (default y)"
              read ans
              case ${ans:=y}
                   N*|n*) return 1 ;;
                   Y*|y*) return 0 ;;
                   *) print "You have entered an invalid option. Try again"
              esac
          done
}

Second way

Code:
confirm_yes()
{
          Valid=1
          while [ $Valid = 1 ]
          do
              echo "${1} [yn,] (default y)"
              read ans
              :      ${ans:=y}
              case ${ans}
                   N*|n*) return 1 ;;
                   Y*|y*) return 0 ;;
                   *) echo "You have entered an invalid option. Try again"
              esac
          done
}

This is for ksh script and function is called by :-

Code:
if confirm_yes "Would you like to unfreeze me?" ;then
        blah blah blah.......
else
        woa woa woa.....
fi

Cheers
# 2  
Old 05-07-2009
: is an alias for "true". See "man true" (though it is also built into ksh). It is a command which does nothing and returns exit status 0. The loop will keep going until it is exited.
In the second example the ":" is being used cleverly as a null command to allow the "${ans:=y}" default substitution to work without giving a syntax error.
# 3  
Old 05-07-2009
In your context the ':' evaluates to 'true' making the 'while' loop an 'endless loop'.
In ksh manual look for 'Special Commands' - you'll see something like:
Code:
: [ arg ... ]
           The command only expands parameters.

# 4  
Old 05-07-2009
Ok thanks.

I'm actually using it in my example then for the while section instead of saying while valid = 1 i'm just saying while true do bla bla

Its just a case of looping then until input is taken.

I personally thing code example one is better but i guess there's not much in it.

cheers
# 5  
Old 05-08-2009
: is a comment that is evaluated. It's not an alias for true really.

: is often times used to set default values for variables.

: ${val:=5}

if val has a value before this, then val is unchanged, if it's empty or not set, val=5

In the olden days before #!/bin/sh, a bourne shell script programmer might begin their script with : at the top to avoid starting the script with #, which meant the script was a csh script.

If you like readability, I sort of like this infinite loop construct:
Code:
while [ T ]; do
....
done

But arguably using : is much more efficient time wise.
# 6  
Old 05-08-2009
Quote:
: is a comment that is evaluated. It's not an alias for true really.
No.

login to a ksh shell,
Code:
alias | grep true
# or try
type true

see what you get.

I don't have bash handy to verify : in that shell.
# 7  
Old 05-08-2009
Quote:
Originally Posted by jim mcnamara
No.

login to a ksh shell,
Code:
alias | grep true
# or try
type true

see what you get.

I don't have bash handy to verify : in that shell.
No, it's neither a comment nor an alias (well, maybe in your case it's). My 'alias | grep true' returns nothing. And 'type true' returns 'true is a shell builtin'.

As the 'Good Book' (man ksh) says, ':' is a 'Special Command' on the same level as '.', 'alias', 'bg', 'break', eval and exec
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep the only instance name

Hi, I want to get the only application name from the server. Ex: if i give $ ps -ef | grep bw. It will show all BW process with entire path. It will little confuse to list out the process. Can anyone have syntax to get only the instance name. I need this for be, hawk,ems also. Please... (2 Replies)
Discussion started by: ckchelladurai
2 Replies

2. Shell Programming and Scripting

Keep the last instance of the record

Hi All, I have a input file like 1| abc 1| abcd 1| abcde 2| abc 2| abcd 3| abcde I want the output like 1| abcde 2| abcde Any help would be highly appreciated. Thanks in advance. (9 Replies)
Discussion started by: lrkp
9 Replies

3. Red Hat

Apache instance

Hi , Maximum How many instances of apache can we run in one box? (2 Replies)
Discussion started by: krish4linux
2 Replies

4. Shell Programming and Scripting

Sed on first instance only

Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like: stuff morestuff 0 otherthing 0 etc I want to substitute for the 0 but what I want to... (9 Replies)
Discussion started by: eltinator
9 Replies

5. Shell Programming and Scripting

matching first instance of FS

Hi All, I have a property in a file as: property=value=a If I use FS="=" then I want only first = to be considered as field separator and remaining as value echo -e "property=value=a" | awk -F= '{print $2}' ie my $2 should be value=a Can anyone please help me with this. I need it in... (3 Replies)
Discussion started by: gurukottur
3 Replies

6. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

7. Shell Programming and Scripting

Single Instance

Hi, I have a script. I want only one instance of the script to be running at any point of the time. How can I do it. what would be the exact format of the ps command for doing this. For example the name of my script is "Inst.sh" Thanx in advance (2 Replies)
Discussion started by: sendhil
2 Replies

8. Linux

OTRS instance

hi frnds here i m trying to configure OTRS instance but i m getting the following error message while runnning through browser. I m writing the following http://192.168.1.55:8080/otrs2/index.pl " #!/usr/bin/perl -w... (7 Replies)
Discussion started by: naik_mit
7 Replies

9. UNIX for Dummies Questions & Answers

Copy Db Instance

I need to copy my Live Db Instance to my Test Db Instance Can somebody please tell me the easiest way to go about this It is an Informix Database running on HP-UX Thanks (0 Replies)
Discussion started by: cobdeng
0 Replies

10. UNIX for Dummies Questions & Answers

finding first instance

I have a file with 3 columns. Often the first two will hold the same values and the third may differ. I only want one row for each. i.e. aaa,bbb,1 aaa,bbb,2 aaa,bbb,3 xxx,yyy,1 xxx,yyy,2 zzz,rrr,5 Should be aaa,bbb,1 xxx,yyy,1 zzz,rrr,5 I just want the first instance of... (20 Replies)
Discussion started by: peter.herlihy
20 Replies
Login or Register to Ask a Question