Shell脚本:将输出分配给不同的变量

时间:2017-07-25 14:49:38

标签: shell unix

在shell脚本中,我需要将少量值的输出分配给不同的变量,请需要帮助。

cat file1.txt  
uid: user1  
cn: User One  
employeenumber: 1234567  
absJobAction: HIRED  

我需要将每个属性的值分配给不同的变量,以便我可以在脚本中将它们称为它们。例如,应该将uid分配给一个新的变量名称current_uid,当调用$ current_uid时,它应该为user1提供所有其他属性等等。
如果输出不包含任何属性,那么该属性值应被视为" NULL"。例如,如果输出没有absJobAction,则$ absJobAction的值应为" 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  

2 个答案:

答案 0 :(得分:1)

>
=undefined
connected? err= null info= {
  "ip": "192.168.1.105",
  "netmask": "255.255.255.0",
  "gw": "192.168.1.1",
  "mac": "5c:**:7f:**:7a:**"
 }

请注意,必须#!/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]}" 合作,而不是bash yourscript

顺便说一下 - 如果你没有bash 4.0,你可能会考虑采用不同的方法:

sh yourscript

将创建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 ...) "$ldap_cn"形式的单独变量,而不是将所有内容放在单个关联数组中。

答案 1 :(得分:0)

这是一个简单的例子,说明你要做的事情应该让你开始。它假定文件中有一组数据。虽然有点蛮力,但我相信它很容易理解。

在当前目录中给出一个名为file.txt的文件,其中包含以下内容(故意遗漏absJobAction):

$ cat file1.txt
uid: user1  
cn: User One  
employeenumber: 1234567  
$ 

此脚本将每个值都放入一个局部变量并打印出来:

# 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: 
$