- 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
推薦閱讀
- Unreal Engine Physics Essentials
- Instant Testing with CasperJS
- 精通Nginx(第2版)
- 自制編譯器
- Hands-On Image Processing with Python
- Bootstrap Essentials
- HTML5 and CSS3 Transition,Transformation,and Animation
- Functional Kotlin
- MongoDB權威指南(第3版)
- Hands-On Functional Programming with TypeScript
- Learning JavaScript Data Structures and Algorithms
- Struts 2.x權威指南
- 計算機應用基礎(第二版)
- Java編程指南:語法基礎、面向對象、函數式編程與項目實戰
- Mastering Unity 2017 Game Development with C#(Second Edition)