Shell脚本:将输出分配给不同的变量
在shell脚本中,我需要将几个值的输出分配给不同的变量,需要帮助吗Shell脚本:将输出分配给不同的变量,shell,unix,Shell,Unix,在shell脚本中,我需要将几个值的输出分配给不同的变量,需要帮助吗 cat file1.txt uid: user1 cn: User One employeenumber: 1234567 absJobAction: HIRED 我需要将每个属性的值分配给不同的变量,以便在脚本中调用它们。例如,uid应该分配给一个新的变量名current\u uid,当调用$current\u uid时,它应该为所有其他属性提供user1等等。 如果输出不包含任何属性,则该属性值应视为
cat file1.txt
uid: user1
cn: User One
employeenumber: 1234567
absJobAction: HIRED
我需要将每个属性的值分配给不同的变量,以便在脚本中调用它们。例如,uid应该分配给一个新的变量名current\u uid,当调用$current\u uid时,它应该为所有其他属性提供user1等等。如果输出不包含任何属性,则该属性值应视为“NULL”。示例如果输出没有AbjobAction,则$AbjobAction的值应为“NULL”
这就是我对阵列所做的
#!/bin/bash
IFS=$'\n'
array=($(cat /tmp/file1.txt | egrep -i '^uid:|^cn:|^employeenumber|^absJobAction'))
current_uid=`echo ${array[0]} | grep -w uid | awk -F ': ' '{print $2}'`
current_cn=`echo ${array[1]} | grep -w cn | awk -F ': ' '{print $2}'`
current_employeenumber=`echo ${array[2]} | grep -w employeenumber | awk -F ': ' '{print $2}'`
current_absJobAction=`echo ${array[3]} | grep -w absJobAction | awk -F ': ' '{print $2}'`
echo $current_uid
echo $current_cn
echo $current_employeenumber
echo $current_absJobAction
sh/tmp/testscript.sh
的输出如下:
user1
User One
1234567
HIRED
这里有一个简单的例子,说明你正在尝试做什么,应该可以让你开始。它假定文件中有一组数据。虽然有点暴力,但我相信它很容易理解 给定当前目录中名为file.txt的文件,该文件包含以下内容(absJobAction故意省略): 此脚本将每个值放入局部变量并将其打印出来:
# Use /bin/bash to run this script
#!/bin/bash
# Make SOURCEFILE a readonly variable. Make it uppercase to show its a constant. This is the file the LDAP values come from.
typeset -r SOURCEFILE=./file1.txt
# Each line sets a variable using awk.
# -F is the field delimiter. It's a colon and a space.
# Next is the value to look for. ^ matches the start of the line.
# When the above is found, return the second field ($2)
current_uid="$(awk -F': ' '/^uid/ {print $2}' ${SOURCEFILE})"
current_cn="$(awk -F': ' '/^cn/ {print $2}' ${SOURCEFILE})"
current_enbr="$(awk -F': ' '/^employeenumber/ {print $2}' ${SOURCEFILE})"
current_absja="$(awk -F': ' '/^absJobAction/ {print $2}' ${SOURCEFILE})"
# Print the contents of the variables. Note since absJobAction was not in the file,
# it's value is NULL.
echo "uid: ${current_uid}"
echo "cn: ${current_cn}"
echo "EmployeeNumber: ${current_enbr}"
echo "absJobAction: ${current_absja}"
~
运行时:
$ ./test.sh
uid: user1
cn: User One
EmployeeNumber: 1234567
absJobAction:
$
将创建形式为
“$ldap\u cn”
或“$ldap\u uid”
的单独变量,而不是将所有内容都放在一个关联数组中。我认为您最好搜索一下初学者的shell脚本。有大量的可用资源。这个论坛更多的是针对具体问题提供帮助。@Gary_W我查看了一些注释,分配单个变量非常简单,但对于多变量,它被要求使用数组,我尝试了一些事情,但没有按照我想要的方式工作。我是这些的初学者。无论如何,谢谢。你应该在你的帖子中提到这一点,展示你尝试过的和没有成功的。因为你的问题太笼统了。如果你的输入格式很简单,你应该考虑改写它:<代码>=<代码>,而不是<代码>:。也就是说,将其设为uid=user1
,并在执行时执行赋值。file1.txt
@Binish。到目前为止,您尝试了什么…?这是可行的,但与只进行一次传递并使用shell内置程序读取多个变量相比,这确实是低效的。@Gary\u W谢谢,它奏效了。非常感谢您在这方面花费的时间和精力。关于您的脚本的一个问题是,如果我的输出直接来自ldap命令而不是文件,您的脚本将进行哪些更改?这是为了按照您的原始规范从文件中获取值而设置的。重写将使其成为一个全新的程序,在这种情况下,达菲先生的解决方案已经很好地发挥了作用。一种方法类似于:result=$(ldapsearch…
),然后是current\u uid=$(awk-F':'/^uid/{print$2}“非常感谢,它成功了。我将ldap搜索直接重定向到while循环,而不是文件名,这也很有效。我非常感谢您为我所做的一切。
$ ./test.sh
uid: user1
cn: User One
EmployeeNumber: 1234567
absJobAction:
$
#!/usr/bin/env bash
# assuming bash 4.0 or newer: create an associative array
declare -A vars=( )
while IFS= read -r line; do ## See http://mywiki.wooledge.org/BashFAQ/001
if [[ $line = *": "* ]]; then ## skip lines not containing ": "
key=${line%%": "*} ## strip everything after ": " for key
value=${line#*": "} ## strip everything before ": " for value
vars[$key]=$value
else
printf 'Skipping unrecognized line: <%s>\n' "$line" >&2
fi
done <file1.txt # or < <(ldapsearch ...)
# print all variables read, just to demonstrate
declare -p vars >&2
# extract and print a single variable by name
echo "Variable uid has value ${vars[uid]}"
while IFS= read -r line; do
if [[ $line = *": "* ]]; then
key=${line%%": "*}
value=${line#*": "}
printf -v "ldap_$key" %s "$value"
fi
done <file1.txt # or < <(ldapsearch ...)