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

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
主站蜘蛛池模板: 乌兰察布市| 大邑县| 津南区| 和林格尔县| 兰坪| 泗水县| 疏勒县| 武陟县| 沧源| 绍兴市| 普定县| 涿鹿县| 商水县| 小金县| 迁安市| 浮山县| 海伦市| 牡丹江市| 锦屏县| 峨边| 松桃| 海盐县| 孙吴县| 云霄县| 富锦市| 壶关县| 乡宁县| 宜兰市| 什邡市| 信丰县| 太仆寺旗| 睢宁县| 唐山市| 贡觉县| 平邑县| 汤原县| 同江市| 西吉县| 钟山县| 阳泉市| 岳池县|