官术网_书友最值得收藏!

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
主站蜘蛛池模板: 白城市| 临泉县| 仙居县| 禄劝| 安新县| 凌海市| 开鲁县| 潢川县| 左贡县| 军事| 格尔木市| 利津县| 泗洪县| 达日县| 互助| 张家界市| 綦江县| 普兰店市| 山东| 西峡县| 左云县| 平乐县| 沙雅县| 卢湾区| 吉首市| 灵台县| 泾源县| 绍兴县| 县级市| 来宾市| 吴堡县| 红桥区| 霍林郭勒市| 万安县| 平度市| 福鼎市| 宜城市| 揭东县| 鲁山县| 瑞安市| 栾城县|