UNIX function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX function
# 1  
Old 12-11-2012
UNIX function

I have a code like this

Code:
v_time=12:12:12
correctTimeFlag=$(echo $v_time|awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9
]:[0-5][0-9]:[0-5][0-9]/){print i}}')

if [ correctTimeFlag -gt 0 ]
then
  echo "correct"
fi

I need a equivalent function to replace the line

Code:
correctTimeFlag=$(echo $v_time|awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9
]:[0-5][0-9]:[0-5][0-9]/){print i}}')

by something like
Code:
correctTimeFlag=$( chkTimeFormat $v_time)

Please help
# 2  
Old 12-11-2012
Code:
 
chkTimeFormat()
{
 time="$1"
 echo "$time" | grep -v "^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$" >/dev/null && echo "0" || echo "1"
}

# 3  
Old 12-11-2012
How about:
Code:
case $v_time in 
  [0-2][0-9]:[0-5][0-9]:[0-5][0-9])
     echo "correct"
esac

--
Code:
correctTimeFlag() {
  case $1 in
    [0-2][0-9]:[0-5][0-9]:[0-5][0-9]) return 0
  esac
  return 1
}

if correctTimeFlag "$v_time"; then
  echo "\$v_time is correct"
fi


Last edited by Scrutinizer; 12-11-2012 at 05:07 AM..
# 4  
Old 12-11-2012
Code:
check_time () {
[[ $(echo $v_time|awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/){print i}}') ]] && echo "correct" || echo "0"
}

Code:
function get_time () {
echo $v_time | awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/){print i}}'
}
v_time="12:12:12"

correctTimeFlag=$(get_time)


Last edited by pamu; 12-11-2012 at 05:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Function in UNIX

Hi - Have file1 which has the below date 08/31/2018 And also have file2 which has the below texts ASOF:<CMODate> FUND I need to read the second file if it has colon (:) then move the date from first file to second file like this ASOF:08/31/2018 have used cut -d":" -f1 and moved the... (2 Replies)
Discussion started by: Mohan0509
2 Replies

2. Shell Programming and Scripting

UNIX Date function with -d options

Hi , I couldn't understand how this program works. Can somebody please explain it to me. DT=`date -d "1 day"` # This part I understand echo ${DT/ */} # How this works echo ${DT/* /} (2 Replies)
Discussion started by: LoneRanger
2 Replies

3. Shell Programming and Scripting

Split function in HP UNIX

hi all, i have large file. where i need the split the files into two files. can anyone tell me what is the command for that? Unix OS :HP Unix. Thanks, arun. (2 Replies)
Discussion started by: arun888
2 Replies

4. Shell Programming and Scripting

Sort function UNIX bug ???

Hello there i have a funny behiavor of the sort fonction, i try it out on different Solaris machine and i have the same issue. So i would like to see if there is a rationel explanation here is some data in a file:test.txt ,Test,RSD,RSD_Asset ,Test,RSD,RSD_Credit ,Test,RSD,RSD_Liab... (3 Replies)
Discussion started by: kykyboss
3 Replies

5. Solaris

function parameter in unix

Hi, How to use a function with passing value as a parameter in unix ? With Regards (7 Replies)
Discussion started by: milink
7 Replies

6. Shell Programming and Scripting

Recursive function in unix

Can someone tell me, how do i write a recursive code using shell ( eg like 'for' loop in C) which outputs the record to a database table as one row per iteration? (7 Replies)
Discussion started by: goutam_igate
7 Replies

7. Shell Programming and Scripting

function in UNIX

Hi, I have an array of data as below: df = trx df = xml I would like to create a function which contains array inside it and the value is determined when the function is called as below: function pass_or_fail { if } ] then echo " ${df} FAILED. Please check logs" ... (8 Replies)
Discussion started by: luna_soleil
8 Replies

8. UNIX for Dummies Questions & Answers

Trouble with UNIX tr (translate) function

UNIX script - problem. I want the spaces in my Item variable to be replaced with a question mark. Can you tell me what I am doing wrong? This is the whole code line. Item | tr -s " " "?" Why is this not making any changes to the Item value? Thanks for any help you can give! tg (2 Replies)
Discussion started by: by_tg
2 Replies

9. Cybersecurity

Function of Javascript within Unix Network

What attacks can a Unix box get through Javascript? Is the Web Client secure against Javascript attacks if any? Do we have a Trojan horse made in JavaScript? (3 Replies)
Discussion started by: netass
3 Replies

10. Programming

decrypt function in unix?

Hai Friends We have a function called char * crypt(const char *ps, const char* key) to encrypt the password in the unix environment.. Do we have any decrypt function to decrypt it? If not then how can we verify the password through program? Thanks in Advance Collins (1 Reply)
Discussion started by: collins
1 Replies
Login or Register to Ask a Question