- Bash Cookbook
- Ron Brash Ganesh Naik
- 105字
- 2021-07-23 19:17:31
Using a function with parameters within a for loop
In this short example, we have a function called create_file, which is called within a loop for each file in the FILES array. The function creates a file, modifies its permissions, and then passively checks for its existence using the ls command:
#!/bin/bash
FILES=( "file1" "file2" "file3" ) # This is a global variable
function create_file() {
local FNAME="${1}" # First parameter
local PERMISSIONS="${2}" # Second parameter
touch "${FNAME}"
chmod "${PERMISSIONS}" "${FNAME}"
ls -l "${FNAME}"
}
for ELEMENT in ${FILES[@]}
do
create_file "${ELEMENT}" "a+x"
done
echo "Created all the files with a function!"
exit 0
推薦閱讀
- HTML5+CSS3王者歸來
- 劍指Offer(專項突破版):數據結構與算法名企面試題精講
- R語言游戲數據分析與挖掘
- Apache Spark 2 for Beginners
- Learning AWS Lumberyard Game Development
- 網店設計看這本就夠了
- 程序設計基礎教程:C語言
- Clojure Reactive Programming
- Python Interviews
- 人工智能算法(卷1):基礎算法
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- C++程序設計教程(第2版)
- Magento 2 Beginners Guide
- Flask Web開發:基于Python的Web應用開發實戰(第2版)
- Unity Android Game Development by Example Beginner's Guide