循环浏览文件目录,并为每个迭代值将变量设置到另一个shell脚本请求中

时间:2018-11-26 08:37:22

标签: shell cygwin sh

首先,谢谢您。

shell脚本的新手,想在这里获得一些帮助... 目的是能够通过Shell脚本遍历内容列表,并将它们设置为变量以重复请求调用。具体如下:

因此,我有一个shell脚本(称为runScripts.sh),它将启动该脚本

HOST="host_here" 
USERID="userID_here" 
PASSWORD="password_here"
DOMAIN="domain_here" 
ENV="env_here"

if [ "$ENV" == "env_here" ] then 
   FILENAME="filename1"
   ./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME
   FILENAME="filename2" 
   ./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME 
else echo "Upload Files: NULL ENV" 
fi

它将调用另一个脚本(doCreatedir.sh)来构建SOAP请求并调用服务,如下所示:

user=$1
pass=$2
hostname=$3
domain=$4
dir=$5

{
    cat <<-EOF
                <?xml version="1.0" encoding="UTF-8"?>
                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                    <soapenv:Body>
                        <dp:request xmlns:dp="http://www.datapower.com/schemas/management" domain="$domain">
                            <dp:do-action>
                                <CreateDir>
                                    <Dir>local:///$dir</Dir>
                                </CreateDir>
                            </dp:do-action>
                        </dp:request>
                    </soapenv:Body>
                </soapenv:Envelope>
    EOF
  } | tee request_doCreateDir.xml

CURL_RESPONSE=$(curl --data-binary @request_doCreateDir.xml https://$hostname:5550/service/mgmt/3.0 -k -u $user:$pass)

CURL_RESPONSE_STATUS=$(echo $CURL_RESPONSE | grep 'OK')

if [ "$CURL_RESPONSE_STATUS" = "" ]; then
echo "Failed to create $dir."
exit 1;
fi

echo "Successfully created directory on DataPower."
echo "Domain: $domain"

我不知道如何遍历文件夹,并获得所有名称以将其插入文件名1和文件名2(如下所示)

FILENAME="{filename1}" 
./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME 
FILENAME="{filename2}" 
./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME

,并确保文件名1和文件名2也在doCreatdir.sh中作为dir变量循环访问。

仅供参考->我不需要第二个文件名代码行,如果我可以遍历我想要的文件名...例如,可以删除以下代码...

  

FILENAME="filename2" ./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME

...如果我只能遍历一个文件夹,然后将文件的每个名称输入第一个代码:

  

FILENAME="filename1" ./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME

... 我真正在做的是,不必为我拥有的每个文件一遍又一遍地手动输入以下内容...

FILENAME="{filename1}" 
./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME 
FILENAME="{filename2}" 
./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME

...我只想一行,然后遍历文件夹目录中的所有文件名:

FILENAME="{each_file_name_from_a_source_folder_here}" 
./scripts/doCreatedir.sh $USERID $PASSWORD $HOST $DOMAIN $FILENAME 

谢谢。

0 个答案:

没有答案