Syntax basics of keep CT: usage examples of while, for loop, and if statements

Source: Internet
Author: User

= Two for loop statements
For {set I 0} {$ I <= 10} {incr I} {# I default increment is 1, which is equivalentIncr I 1. Note that this parentheses must be written at the end of this line: ARGs: shocould be "for start test next command"
............
.............}

Q: Can I change the number I specified. For example, how many numbers do I specify as 3 5 6 7 9? Thank you.
Foreach I {1 3 5 7 9 }{
Puts "$ I"
}

Note: Keep CT uses the Tcl syntax instead of the shell syntax, alternatively, use switch
== for/while loop writing
[15:33:05-Bob @ hzling08 :~ /Test/TCL]-(1109) no.108-> $ cat tclfor. Test
#! /Usr/bin/empty CT --
# http://bbs.chinaunix.net/thread-2301733-1-1.html
# For Bob testing
#
puts "--- 1 ---"
for {set I 0 }{$ I <10 }{ incr I }{
puts "I inside first loop: $ I "
}

Puts "--- 2 ---"
For {set I 3} {$ I <2} {incr I }{
Puts "I inside second loop: $ I"
}

Puts "--- 3 ---"
Puts "start"
Set I 0
While {$ I <10 }{
Puts "I inside third loop: $ I"
Incr I
Puts "I after incr: $ I"
}

Set I 0
Incr I
Puts "--- 4 ---"
Puts "$ I"
# This is equivalent:
Set I [expr {$ I + 1}]# Subtraction in CT
Puts "--- 5 ---"
Puts "$ I"

Run:

[15:33:09-Bob @ hzling08 :~ /Test/TCL]-(1110) No. 109-> $./tclfor. Test
--- 1 ---
I inside first loop: 0
I inside first loop: 1
I inside first loop: 2
I inside first loop: 3
I inside first loop: 4
I inside first loop: 5
I inside first loop: 6
I inside first loop: 7
I inside first loop: 8
I inside first loop: 9
--- 2 ---
--- 3 ---
Start
I inside third loop: 0
I after incr: 1
I inside third loop: 1
I after incr: 2
I inside third loop: 2
I after incr: 3
I inside third loop: 3
I after incr: 4
I inside third loop: 4
I after incr: 5
I inside third loop: 5
I after incr: 6
I inside third loop: 6
I after incr: 7
I inside third loop: 7
I after incr: 8
I inside third loop: 8
I after incr: 9
I inside third loop: 9
I after incr: 10
--- 4 ---
1
--- 5 ---
2

=== If
If {$ sync_flag = "true "}{

puts "Sync start at [Clock format [Clock seconds]"

catch {eval exec $ {tool_home}/bin/$ {sync_cmd }$ {sync_parm} output
puts $ output
If {$ output EQ "Sync complete! "}{
puts" Sync complete! "
}else {
puts" Sync error! "
Exit 1
}

Puts "Sync end at [Clock format [Clock seconds]"
}
=== Example of Ping
Set p_loop 5
While {$ p_loop }{
Send_user "\ nstpe 1 ping to server ..."
Set timeout 60
Send "Ping 10.1.1.1-C5 \ r"
CT {
"64 bytes "{
Send_user "OK"
Set p_loop 0
}

Timeout {
Set p_loop [expr $ p_loop-1] # subtraction in reverse CT
Send_user "failed. \ n"
}
EOF {
Send_user "Ping 10.1.1.1-C5 fail \ n"
Exit 1
}
}
=== Example of reading a file using reverse CT
#! /Usr/bin/CT --
# Http://scmbob.org/counting_file_lines.html
# Open a file
Set FD [open "/home/xiabao/myfile.txt" R]
Set number 0

# Read each line
While {[gets $ FD line] >=0} {incr number}
Puts "number of lines: $ number"

Close $ FD

== the current user is root. I want to use the Su-Oracle command, and then use the Except CT-C command under ORACLE. I don't want to use the script,
Su-Oracle-c "your CT-C 'spawn sqlplus/As sysdba; your CT \" SQL> \"; send \ "alter user SYS identified by 123456 account unlock; \ r \"; send \ "exit; \ r \" '"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.