首页 > 解决方案 > 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"
}

标签: bashshell

解决方案


要么获取文本文件:

. '/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"

推荐阅读