Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

Command-Line Arguments ใน Bash Scripts: บทช่วยสอนปฏิบัติจริง

บทช่วยสอนนี้อธิบายวิธีส่ง command-line arguments ให้ bash scripts ของคุณ มีตัวอย่างมากมายช่วยให้เริ่มต้นได้

Bash/Shell scripts เป็นวิธียอดเยี่ยมในการ automate Linux workflow และเร่งความเร็ววันทำงาน เพื่อให้คุณออกจากสำนักงานเร็ว (หรือซ่อนใน server room จน 5 โมงเย็น)

การทำให้ scripts reusable ทำให้มันมีประโยชน์มากขึ้น – คุณไม่ต้องเขียน script ที่ทำงานเดียวกันสำหรับชุดข้อมูลต่างกันหรือวันต่างๆ – คุณสามารถนำ script เดียวกันมาใช้ซ้ำและให้ parameters ต่างๆ – คำแนะนำที่ส่งให้ script ผ่าน command line นี่บางครั้งเรียกว่า command line arguments

เช่น คุณอาจมีสคริปต์ output ข้อความลงไฟล์ หากต้องการเปลี่ยนชื่อไฟล์ ต้อง edit script หากต้องการ output หลายไฟล์ อาจต้องมีหลาย copies ของ script ด้วย parameters คุณจะมี single script ที่ output ไปไฟล์ไหนก็ได้ ชื่ออะไรก็ได้ ที่ส่งเป็น parameters จาก command line

มีหลายวิธีส่ง parameters นี้ – options และ values – ให้ script ขึ้นอยู่กับเป้าหมาย นี่คือหลายวิธีพร้อมตัวอย่าง:

วิธี getopts / Flags สำหรับอ่าน Command Line Input

  • getopts เป็น shell command ที่ดึง options (หรือ flags) จาก command line input และทำ value (หรือ argument) ให้ใช้ได้ใน scripts
    • Options คือตัวอักษรเดี่ยวขึ้นต้นด้วย – (dash) ตามด้วย value เช่น -v value
  • เหมาะสำหรับกรณีที่ลำดับไม่สำคัญ
  • อาจเป็นวิธีที่ดีที่สุดในทุกวิธีที่ระบุไว้ที่นี่

ตัวอย่าง getopts Bash script

ตัวอย่าง script นี้รับสาม options สำหรับ fast-food order:

  • -o สำหรับ order
  • -s สำหรับ size
  • -d สำหรับ drink

fastFoodOrder.sh

#!/bin/bash # Use a while loop to parse through the options o, s, d while getopts o:s:d: flag do # Match each option (which has been assigned to the $flag variable) to it's meaning using a case statement block # OPTARG will be the value received from getopts for the corresponding option and is assigned to a variable depending on the flag case "${flag}" in o) order=${OPTARG};; s) size=${OPTARG};; d) drink=${OPTARG};; esac done # The values as passed to the options are now available in their corresponding variable echo "Order: $order";; echo "Size: $size";; echo "Drink: $drink";;

คลิกที่นี่เพื่อดูเพิ่มเติมเกี่ยวกับบรรทัด #!/bin/bash ต้น頭ของ bash scripts

เพื่อรัน ให้ใส่:

bash ./fastFoodOrder.sh -d Cola -o 'Fish Burger Meal' -s Large

ซึ่งจะทำให้ script output:

Order: Fish Burger Meal Size: Large Drink: Cola

เห็นได้ใน output ด้านบน – ลำดับที่ options ส่งมาไม่สำคัญ

ทำไมต้องใช้ ./ ตอน execute scripts?

ส่งหลายค่าให้ Option เดียวกัน

เป็นไปได้ที่จะส่ง option เดียวกันหลายครั้งสำหรับหลายค่า – พวกมันจะพร้อมใช้งานใน script เป็น array

นี่คือวิธีที่ดีที่สุดสำหรับส่งหลายค่าให้ option เดียว เพราะอนุญาตให้ใช้ตัวอักษรใดๆ ใน value – การส่งค่าใน quotes หรือใช้ delimiter จะจำกัดสิ่งนี้

fastFoodOrderExtras.sh

#!/bin/bash # Use a while loop to parse through the options o, s, d, e while getopts o:s:d:e: flag do # Match each option (which has been assigned to the $flag variable) to it's meaning using a case statement block # OPTARG will be the value received from getopts for the corresponding option and is assigned to a variable depending on the flag case "${flag}" in o) order=${OPTARG};; s) size=${OPTARG};; d) drink=${OPTARG};; e) extras+=${OPTARG};;# Create or append an array variable to hold the multiple extras values passed esac done # The values as passed to the options are now available in their corresponding variable echo "Order: $order";; echo "Size: $size";; echo "Drink: $drink";; echo "The list of extras is '${extras[@]}'" # Loop through the multiple extras values for val in "${extras[@]}";; do echo $val done

สคริปต์ข้างต้นรับ multiple -e options สำหรับเพิ่ม extras ตามด้านล่าง:

bash ./fastFoodOrderExtras.sh -d Cola -o 'Fish Burger Meal' -s Large -e Sauce -e Salt -e Pepper

Script จะ output:

Order: Fish Burger Meal Size: Large Drink: Cola The list of extras is 'SauceSaltPepper' SauceSaltPepper

การใช้ Positional Parameters ใน Scripts

  • ใช้เมื่อมีจำนวน arguments/parameters ที่รู้จักล่วงหน้าในลำดับเฉพาะ
  • เหมาะสำหรับ scripts ที่ง่ายกว่า

ตัวอย่าง

fastFoodOrder.sh

#!/bin/bash echo "Order: $1";; echo "Size: $2";; echo "Drink: $3";;

เพื่อรัน ให้ใส่:

bash ./fastFoodOrder.sh 'Fish Burger Meal' Large Cola

ซึ่งจะทำให้ script output:

Order: Fish Burger Meal Size: Large Drink: Cola

โปรดทราบว่า หาก options ถูกส่งมาลำดับผิด ค่าทั้งหมดจะสับสนกัน Position matters ด้วยวิธีนี้!

bash ./fastFoodOrder.sh 'Fish Burger Meal' Cola Large

จะทำให้ค่าถูก assign ผิด:

Order: Fish Burger Meal Size: Cola Drink: Large

การใช้ Loops อ่าน Command Line Parameters

  • เหมาะสำหรับกรณีที่จำนวน options/arguments ไม่รู้ล่วงหน้า
    • เช่น คุณอาจส่งรายการ ingredients สำหรับสูตรอาหาร
  • ใช้ built-in variable admin@wsxdn.com, ซึ่งมี array ของ input options ทั้งหมดที่ user ส่งมา

ตัวอย่าง

recipe.sh

#!/bin/bash i=1; # This value is the iterator value - the current number of loops that have completed for ingredient in "admin@wsxdn.com" # loop through admin@wsxdn.com which is an array of all passed input parameters do echo "Ingredient number $i is $ingredient";;# Print the $ingredient which was passed to the script when it was called i=$((i + 1)); # Increment the iterator value so we know how many times we have looped done

Run it:

sh ./recipe.sh bread lettuce cheese hamburger

ซึ่งจะ output:

Ingredient number 1 is bread Ingredient number 2 is lettuce Ingredient number 3 is cheese Ingredient number 4 is hamburger