在Bash中将字符串拆分为多个变量 [英] Split string into multiple variables in Bash

查看:155
本文介绍了在Bash中将字符串拆分为多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面生成了一个字符串:

I have a string that gets generated below:

192.168.1.1,UPDOWN,Line protocol on Interface GigabitEthernet1/0/13, changed state to up

我如何才能使用该字符串并使用bash使其生成2个变量?

How can I take that string and make 2 variables out of it (using bash)?

例如我想要

$ip=192.168.1.1 
$int=GigabitEthernet1/0/13

推荐答案

尝试一下:

mystring="192.168.1.1,UPDOWN,Line protocol on Interface GigabitEthernet1/0/13, changed state to up"

IFS=',' read -a myarray <<< "$mystring"

echo "IP: ${myarray[0]}"
echo "STATUS: ${myarray[3]}"

在此脚本中,${myarray[0]}表示逗号分隔的字符串中的 first 字段,${myarray[1]}表示逗号分隔的字符串中的 second 字段,等

In this script ${myarray[0]} refers to the first field in the comma-separated string, ${myarray[1]} refers to the second field in the comma-separated string, etc.

这篇关于在Bash中将字符串拆分为多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆