bash - Execute function with arguments listed in text file
问题描述
I have a text file (note - first 3 lines are blank) which has function with arguments value specified as shown below.
Text File:
Function_1 <arguments_1> <arguments_2> <arguments_3> <arguments_4>
Function_2 <arguments_1> <arguments_2> <arguments_3> <arguments_4>
Function_3 <arguments_1> <arguments_2> <arguments_3> <arguments_4>
In a Shell script, I have the function definition.
Requirement is to read the content (i.e. Function with argument value) from 4th line available in the text file and exec the function present in shell script.
Shell script:
#!/bin/bash
logfile=Execution_$now.log
exec 2>&1 | tee $logfile
Text_File = <path>
while IFS= read -r line
do
echo "$line"
done < "$Text_File"
Function_1()
{
exec &> $1_$(date "+%Y%m%d%H%M").log
Change=$1
Command=$2
if [[ $Command == "get" ]];
then Command_1="getfacl"
elif [[ $Command == "set" ]];
then Command_1="setfacl"
fi
Tag=$3
if [[ -z "$Tag" ]];
then Tag=""
else Tag="-$Tag"
fi
echo " "
echo "#############"
echo "Test Script"
echo "#############"
echo "Simply testing the script execution"
}
解决方案
要么获取文本文件:
. '/path/to/file'
或者
source '/path/to/file'
或者将您的文件作为数组读取,因此每个命令和参数都由IFS
环境变量正确拆分。
text_file='/path/to/file'
declare -a command_and_args_array
while read -r -a command_and_args_array # Read a command with arguments line
do
"${command_and_args_array[@]}" # Execute the command with its arguments
done < "$text_file"
推荐阅读
- c - 如何用 C 制作交互式菜单?
- javascript - 来自 momentjs 的函数 fromNow() 和 toNow() 无法按预期工作
- python - 在时间戳边界之间填充 Pandas 列
- react-native - React Native(Android):找不到屏幕“Home”的“component”、“getComponent”或“children”道具
- javascript - 我不断收到关于 discord.js 中 .position 的错误
- cassandra - com.datastax.oss -> java-driver-core 和 com.datastax.cassandra -> cassandra-driver-core 之间的 Cassandra 区别
- beautifulsoup - BeautifulSoup - 餐桌
- java - GraphStream 2.0 - 拖动节点时鼠标指针偏移
- python - 仅从 df 中提取值
- perl - Perl script doesn't see modules .pm