Java 在一个文件中存储具有相同键的多个数据并从shell脚本获取值?
我从json(如dis)获得输出Java 在一个文件中存储具有相同键的多个数据并从shell脚本获取值?,java,bash,shell,properties,Java,Bash,Shell,Properties,我从json(如dis)获得输出 {user=xxx, ip=xxxxx}, {user=yyy, ip=yyyyy} etc . 我将此数据存储在属性文件中(从shell脚本访问此信息) 如果我试着这么做,它会像 user=xxx ip=xxxxx user=yyy ip=yyyyy 但是,在属性文件中,键值应该是唯一的。如果是这种情况,我无法从属性文件访问此数据 脚本中的实际需求是我必须使用相应的用户连接到Ip。每个用
{user=xxx, ip=xxxxx},
{user=yyy, ip=yyyyy} etc .
我将此数据存储在属性文件中(从shell脚本访问此信息)
如果我试着这么做,它会像
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
但是,在属性文件中,键值应该是唯一的。如果是这种情况,我无法从属性文件访问此数据
脚本中的实际需求是我必须使用相应的用户连接到Ip。每个用户都必须进行同样的操作
除了属性文件之外,是否还有其他存储数据的方法来访问shell脚本中的值
谁能给我介绍一下吗?是的,你说得对。Java属性文件不是您的选择,因为如果相同的密钥到达,它们将覆盖旧条目。但是写一些类似的东西来处理这种情况并不难。
看看这个班
public class SimpleStorage{
private String[] entryNames; // names of entries in one dataset
private int amountEntries; // amount of entries in one dataset
private ArrayList<String[]> data; // list of all datasets
public SimpleStorage(String... _entryNames){
entryNames = _entryNames;
amountEntries = entryNames.length;
data = new ArrayList<>();
}
public void put(String... dataSet){
if(dataSet.length == amountEntries)
data.add(dataSet);
else
System.out.println("Wrong amount of dataSet entries. " +
"Should be " + amountEntries + ".");
}
public void save(String filename) throws FileNotFoundException, UnsupportedEncodingException{
PrintWriter writer = new PrintWriter(filename, "UTF-8");
// for all datasets
for(String[] dataSet : data){
// write all entries in form NAME=ENTRY
for(int i = 0; i < amountEntries; i++){
writer.println(entryNames[i] + "=" + dataSet[i]);
}
}
writer.close();
}
}
您将获得包含以下内容的输出文件:
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
是的,你是对的。Java属性文件不是您的选择,因为如果相同的密钥到达,它们将覆盖旧条目。但是写一些类似的东西来处理这种情况并不难。
看看这个班
public class SimpleStorage{
private String[] entryNames; // names of entries in one dataset
private int amountEntries; // amount of entries in one dataset
private ArrayList<String[]> data; // list of all datasets
public SimpleStorage(String... _entryNames){
entryNames = _entryNames;
amountEntries = entryNames.length;
data = new ArrayList<>();
}
public void put(String... dataSet){
if(dataSet.length == amountEntries)
data.add(dataSet);
else
System.out.println("Wrong amount of dataSet entries. " +
"Should be " + amountEntries + ".");
}
public void save(String filename) throws FileNotFoundException, UnsupportedEncodingException{
PrintWriter writer = new PrintWriter(filename, "UTF-8");
// for all datasets
for(String[] dataSet : data){
// write all entries in form NAME=ENTRY
for(int i = 0; i < amountEntries; i++){
writer.println(entryNames[i] + "=" + dataSet[i]);
}
}
writer.close();
}
}
您将获得包含以下内容的输出文件:
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
您似乎希望通过shell脚本中的文件使用其密码访问所有用户 一种可能的解决方案是将用户名作为密钥,密码作为值存储在属性文件中 属性文件
您的.properties
xxx=xxxxx
yyy=yyyyy
zzz=zzzzz
user=www
ip=wwwww
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
shell脚本
#!/bin/ksh
while read line.
do
user=${line%%=*}
ip=${line##*=}
# example how to access the values
printf "user: %s IP: %s\n" "${user}" "${ip}"
done < your.properties
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
#!/bin/sh
# the file input from file `your.properties` is redirected to filedescriptor 3
# to avoid problems with programs inside the loop which themself use `stdin`
# for reading input
while read user <&3; read ip <&3
do
user=${user##*=}
ip=${ip##*=}
# example how to access the values
printf "user: %s ip: %s\n" "${user}" "${ip}"
done 3< your.properties
user: www ip: wwwww
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
如果无法控制属性文件的格式,请编辑。一个解决办法可能是
属性文件您的.properties
xxx=xxxxx
yyy=yyyyy
zzz=zzzzz
user=www
ip=wwwww
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
shell脚本
#!/bin/ksh
while read line.
do
user=${line%%=*}
ip=${line##*=}
# example how to access the values
printf "user: %s IP: %s\n" "${user}" "${ip}"
done < your.properties
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
#!/bin/sh
# the file input from file `your.properties` is redirected to filedescriptor 3
# to avoid problems with programs inside the loop which themself use `stdin`
# for reading input
while read user <&3; read ip <&3
do
user=${user##*=}
ip=${ip##*=}
# example how to access the values
printf "user: %s ip: %s\n" "${user}" "${ip}"
done 3< your.properties
user: www ip: wwwww
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
您似乎希望通过shell脚本中的文件使用其密码访问所有用户 一种可能的解决方案是将用户名作为密钥,密码作为值存储在属性文件中 属性文件
您的.properties
xxx=xxxxx
yyy=yyyyy
zzz=zzzzz
user=www
ip=wwwww
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
shell脚本
#!/bin/ksh
while read line.
do
user=${line%%=*}
ip=${line##*=}
# example how to access the values
printf "user: %s IP: %s\n" "${user}" "${ip}"
done < your.properties
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
#!/bin/sh
# the file input from file `your.properties` is redirected to filedescriptor 3
# to avoid problems with programs inside the loop which themself use `stdin`
# for reading input
while read user <&3; read ip <&3
do
user=${user##*=}
ip=${ip##*=}
# example how to access the values
printf "user: %s ip: %s\n" "${user}" "${ip}"
done 3< your.properties
user: www ip: wwwww
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
如果无法控制属性文件的格式,请编辑。一个解决办法可能是
属性文件您的.properties
xxx=xxxxx
yyy=yyyyy
zzz=zzzzz
user=www
ip=wwwww
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
shell脚本
#!/bin/ksh
while read line.
do
user=${line%%=*}
ip=${line##*=}
# example how to access the values
printf "user: %s IP: %s\n" "${user}" "${ip}"
done < your.properties
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
#!/bin/sh
# the file input from file `your.properties` is redirected to filedescriptor 3
# to avoid problems with programs inside the loop which themself use `stdin`
# for reading input
while read user <&3; read ip <&3
do
user=${user##*=}
ip=${ip##*=}
# example how to access the values
printf "user: %s ip: %s\n" "${user}" "${ip}"
done 3< your.properties
user: www ip: wwwww
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
如何在shell脚本中使用这些值?您是否只需要特定用户的IP地址?
properties
文件是必须的还是您可以自由地在文件中以不同的格式存储数据?我需要同时使用IP和用户。我将使用这些值使用sshpass连接到该IP。无属性不必,可以以不同格式存储。问题是必须从脚本访问。这些值将如何在shell脚本中使用?您是否只需要特定用户的IP地址?properties
文件是必须的还是您可以自由地在文件中以不同的格式存储数据?我需要同时使用IP和用户。我将使用这些值使用sshpass连接到该IP。无属性不必,可以以不同格式存储。问题是必须从脚本访问。如果这是输出,就不可能从脚本获得正确的值。例如,我将尝试为用户获取信息,它将从Output.txt考虑哪个用户?这就是我面临的问题。在写入属性/文本文件之前,是否有可能更改密钥名称?我仍然不明白。您希望获取与一个ip对应的所有用户。或者想要所有IP都对应于一个用户?这里的用户是指用户名,sry误称。对于一个IP,一个用户名将是der,可能相同或不同。我需要从属性文件/或任何其他文件中获取用户名和IP,以便使用sshpass在脚本中使用用户名连接到该IP(所有IP的密码都相同。因此无需获取该密码)。如果这是输出,则无法从脚本中获取正确的值。例如,我将尝试为用户获取信息,它将从Output.txt考虑哪个用户?这就是我面临的问题。在写入属性/文本文件之前,是否有可能更改密钥名称?我仍然不明白。您希望获取与一个ip对应的所有用户。或者想要所有IP都对应于一个用户?这里的用户是指用户名,sry误称。对于一个IP,一个用户名将是der,可能相同或不同。我需要从属性文件/或任何其他文件中获取用户名和IP,以便使用sshpass在脚本中使用用户名连接到该IP(所有IP的密码都相同。因此无需获取该密码)。属性文件本身中的输出类似于用户:xxx IP:xxxxx用户:yyy IP:yyyyy用户:zzz IP:zzzzz
,从这个文件中,我需要获得用户值和IP值。如果我从脚本访问,它会考虑哪个用户/ IP?这就是我要问的。@Vidya你是说你不能控制属性文件的格式?如果是,请检查我的最新答案。谢谢。是的,在我给出sshpass之前,它已经迭代了3次。现在我包括了sshpass,如下所示,#/读取用户时的bin/sh;读取ip do user=${user##*=}ip=${ip##*=}sshpass-p sas ssh-o StrictHostKeyChecking=no$user@$ip“mkdir vidya”printf用户:%s ip:%s\n”“${user}”“${ip}”完成
输出为:user:xxx ip:xxxxx仅迭代一次。我错过了什么吗?@Vidya我修改了我的第二个例子。属性文件现在被重定向到文件描述符3,而不是sshpass也使用的stdin。sshpass正在读取属性文件中的值。谢谢。工作正常。代替3(静态数字),我可以给变量名也正确,这是持有数字!!!!原因是,不知道到底有多少台服务器..我在属性文件本身中的输出类似于用户:xxx ip:xxxxx用户:yyy ip:yyyy用户:zzz ip:zzzzz
,从这个文件中我需要获得用户值和ip值。如果我能