BASH、PERL、PYTHON、TCL之数字前端脚本语言

最近由于工作的需要接触了脚本语言,现总结下来以供后面查询,主要包括了基本的语法及函数与文件的读写操作。本博客内容来自或者部分来自以下网站Bash,Perl,Python3,Python3,Tcl。(特此声明:由于Python不同版本之间语法具有差异,本博客内容全部取自于Python3)

注释

单行注释
bash   ==> #
perl   ==> #
python ==> #
tcl    ==> #

多行注释
bash   ==>
:<
=pod
codes to comment
=cut
python ==>
'''
codes to comment
'''
tcl    ==>
if 0 {
codes to comment
}

 变量

bash   ==>  var1 = 3.1415926 
            echo $var1
            #双引号增加了对变量的引用,可以增加转义字符,单引号直接对内容进行输出
            var2 = "我在双引号 $var1 and \"$var1\"" 
            echo $var2
            var3 = "我在单引号 $var1 and \"$var1\"
            echo $var3
perl   ==>  $var1 = 3.1415926;
            print $var1;
            #双引号增加了对变量的引用,可以增加转义字符,单引号直接对内容进行输出
            $var2 = "我在双引号 $var1 and \"$var1\"";
            print $var2;
            $var3 = "我在单引号 $var1 and \"$var1\";
            print $var3;

python ==>  var1 = 3.1415926
            print(var1)

tcl    ==>  set var1 10
            puts $var1

数组

bash   ==>  array   = (A B "C" D)
            array[0]= A
            array[1]= A
            array[2]= "C"
            array[3]= D
            echo ${array[index]}
            #读取所有元素
            ${array[*]}
            ${array[@]}

perl   ==>  @array = (25, 30, 40);
            @array[0] = 25;
            @array[1] = 30;
            @array[2] = 40;
            print $array[0];  

python ==>  list = [1, 2, 3, 4, 5, 6, 7 ]
            print ("list1[0]: ", list[0])

tcl    ==>  set languages(0) Tcl
            set languages(1) "C Language"
            puts $languages(0)
            puts $languages(1)

哈希

bash   ==> 

perl   ==>  %data = ('google', 'google.com', 'runoob', 'runoob.com', 'taobao', 'taobao.com');
            %data = ('google'=>'google.com', 'runoob'=>'runoob.com', 'taobao'=>'taobao.com');
            $data{'google'} = 'google.com';
            $data{'runoob'} = 'runoob.com';
            $data{'taobao'} = 'taobao.com';
 
            print "\$data{'google'} = $data{'google'}\n";
            print "\$data{'runoob'} = $data{'runoob'}\n";
            print "\$data{'taobao'} = $data{'taobao'}\n";

python ==>  dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} 
            print ("dict['Name']: ", dict['Name'])
            print ("dict['Age']: ", dict['Age'])

tcl    ==> 

循环

bash   ==>  #for 循环
            for var in item1 item2 ... itemN
            do
                command1
                command2
                ...
            commandN
            done

            #while 语句
            while condition
            do
                command
            done
perl   ==>  for ( init; condition; increment ){
               statement(s);
            }
            while(condition)
            {
               statement(s);
            }
            foreach var (list) {
                ...
            }
python ==>  for iterating_var in sequence:
               statements(s) 
            while expression:
               statement(s) 
tcl    ==>  for {initialization} {condition} {increment} {
               statement(s);
            }
            while {condition} {
               statement(s)
            }

条件语句

bash   ==>  #流程控制
            if condition
            then
                command1 
                command2
                ...
                commandN 
            fi
            #if else
            if condition
            then
                command1 
                command2
                ...
                commandN
            else
                command
            fi
            #if else-if else
            if condition1
            then
                command1
            elif condition2 
            then 
                command2
            else
                commandN
            fi
            #case
            case 值 in
            模式1)
                command1
                command2
                ...
                commandN
                ;;
            模式2)
                command1
                command2
                ...
                commandN
                ;;
            esac
            #until
            until condition
            do
                command
            done
perl   ==>  #
            if( $a < 20 ){
                printf "a 小于 20\n";
            }
            #if else
            if(boolean_expression){
               # 在布尔表达式 boolean_expression 为 true 执行
            }else{
               # 在布尔表达式 boolean_expression 为 false 执行
            }
            #if else-if else
            if(boolean_expression 1){
               # 在布尔表达式 boolean_expression 1 为 true 执行
            }
            elsif( boolean_expression 2){
               # 在布尔表达式 boolean_expression 2 为 true 执行
            }
            elsif( boolean_expression 3){
               # 在布尔表达式 boolean_expression 3 为 true 执行
            }
            else{
               # 布尔表达式的条件都为 false 时执行
            }
            #unless
            unless(boolean_expression){
               # 在布尔表达式 boolean_expression 为 false 执行
            }
            #一个 switch 语句允许测试一个变量等于多个值时的情况。每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查。switch case 执行是基于 Switch 模块, Switch 模块默认是没有安装的。
            use Switch;
 
            switch(argument){
               case 1            { print "数字 1" }
               case "a"          { print "字符串 a" }
               case [1..10,42]   { print "数字在列表中" }
               case (\@array)    { print "数字在数组中" }
               case /\w+/        { print "正则匹配模式" }
               case qr/\w+/      { print "正则匹配模式" }
               case (\%hash)     { print "哈希" }
               case (\&sub)      { print "子进程" }
               else              { print "不匹配之前的条件" }
            }
python ==>  #决策
            #if
            if expression:
               statement(s) 
            #if else
            if expression:
               statement(s)
            else:
               statement(s)
tcl    ==> #if
            if {boolean_expression} {
               # statement(s) will execute if the boolean expression is true
            }
            #if else
            if {boolean_expression} {
              # statement(s) will execute if the boolean expression is true 
            } else {
              # statement(s) will execute if the boolean expression is false
            }
            #switch
            set grade B;
            switch $grade {
               A {
                     puts "Well done!"
                 }
               B {
                     puts "Excellent!"
                 }

               C {
                     puts "You passed!"
                 }
               F {
                     puts "Better try again"
                 }
               default {
                     puts "Invalid grade"
                 }
            }
            puts "Your grade is  $grade"

函数

bash   ==> funWithReturn(){
            echo "这个函数会对输入的两个数字进行相加运算..."
            echo "输入第一个数字: "
            read aNum
            echo "输入第二个数字: "
            read anotherNum
            echo "两个数字分别为 $aNum 和 $anotherNum !"
            return $(($aNum+$anotherNum))
            }
            funWithReturn
           #age\n";  # 打印平均值
            }
            # 调用函数
            Average(10, 20, 30);

perl   ==> #子程序
           sub Average{
           # 获取所有传入的参数
           $n = scalar(@_);
           $sum = 0;
               foreach $item (@_){
                  $sum += $item;
               }
           $average = $sum / $n;
           print '传入的参数为 : ',"@_\n";           # 打印整个数组
           print "第一个参数值为 : $_[0]\n";         # 打印第一个参数
           print "传入参数的平均值为 : $average\n";  # 打印平均值
           }
           # 调用函数
           Average(10, 20, 30);
python ==> def my_abs(x):
                if not isinstance(x, (int, float)):
                    raise TypeError('bad operand type')
                if x >= 0:
                    return x
                else:
                    return -x

tcl    ==>  #过程
            proc avg {numbers} {
                set sum 0
                foreach number $numbers {
                  set sum  [expr $sum + $number]
	            }
                set average [expr $sum/[llength $numbers]]
                return $average
            }
            puts [avg {70 80 50 60}]
            puts [avg {70 80 50 }]

文件IO

bash   ==>  #输出重定向
            command > file	将输出重定向到 file
            command >> file	将输出以追加的方式重定向到 file。
            #输入重定向
            command < file	将输入重定向到 file

perl   ==>  open(DATA, "file.txt") or die "file.txt 文件无法打开, $!";

python ==>  fo = open("foo.txt", "wb")
            fo.write(string);
            fo.read([count]); 
            fo.close()

tcl    ==>  #写入文件
            set fp [open "input.txt" w+]
            puts $fp "test"
            close $fp
            #读取文件
            set fp [open "input.txt" r]
            set file_data [read $fp]
            puts $file_data
            close $fp

 

你可能感兴趣的:(IC_HDL)