BASH、PERL、PYTHON、TCL之數字前端指令碼語言
阿新 • • 發佈:2019-01-10
最近由於工作的需要接觸了指令碼語言,現總結下來以供後面查詢,主要包括了基本的語法及函式與檔案的讀寫操作。本部落格內容來自或者部分來自以下網站Bash,Perl,Python3,Python3,Tcl。(特此宣告:由於Python不同版本之間語法具有差異,本部落格內容全部取自於Python3)
註釋
單行註釋 bash ==> # perl ==> # python ==> # tcl ==> # 多行註釋 bash ==> :<<BLOCK ....codes to comment BLOCK perl ==> =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 檔案無法開啟, $!";
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