如何通过bash脚本执行或读取xlsx文件数据

问题描述 投票:0回答:1

我有一个 bash 脚本,其工作方式类似于从 CSV 文件在 Azure 门户中部署资源。 但问题是,

是否可以从 xlsx 文件执行相同的工作。

我不知道如何执行。

有人来 6 请帮我一下。

我把我的脚本和 xlsx 留在了这里:

#!/bin/bash
az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # DEV
az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # TEST
# az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # STAGE
# az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # PROD


# Prompt for the CSV file path
read -p "Enter the path to the CSV file for SP to Group Member : " csv_file

# Check if the CSV file exists
if [ -f "$csv_file" ]; then
    # Read CSV file and populate arrays
    while IFS=',' read -r GroupID ServicePrincipal Subscription; do
        csv_row+=("$GroupID;$ServicePrincipal;$Subscription")
    done < "$csv_file"

    # Process Service Principals
    echo " Creating Service Principles to groups .... "

    for row in "${csv_row[@]}"; do
        IFS=';' read -ra ARRAY <<< "$row"
        groupId=${ARRAY[0]}
        sp_name=${ARRAY[1]}
        subscription=${ARRAY[2]}
        spObjId=$(az ad sp list --display-name "$sp_name" --query '[0].id' --output tsv)
        echo $spObjId
        if [ -n "$spObjId" ]; then
            # set the subscription
            az account set --subscription $subscription
            # execute the command against the subscription
            az ad group member add --group "$groupId" --member-id "$spObjId"
            if [ $? -eq 0 ]; then
                echo "Service Principal with Name: $sp_name (Object ID: $spObjId) added to AAD Group '$groupId' successfully."
            else
                echo "Failed to add Service Principal with Name: $sp_name (Object ID: $spObjId) to AAD Group '$groupId'."
            fi
        else
            echo "Service Principal with Name: $sp_name not found."
        fi
    done
else
    echo "CSV file not found at the specified path."
fi
bash azure azure-cli
1个回答
0
投票

我建议最简单的方法是使用相关的包管理器安装 xlsx2csv,或者访问 https://github.com/dilshod/xlsx2csv

然后您可以简单地将 xlsx 文档转换为 csv,然后您的脚本将以相同的方式工作。

或者,xlsx 文档只是 xml 文档的 zip 文件,您可以解压缩 xlsx,然后修改脚本以直接从 xml 填充,或者您可以将 xml 转换为 csv,并运行未经编辑的脚本。

这应该可以开始。

© www.soinside.com 2019 - 2024. All rights reserved.