Bash通过具有行和列的变量循环 [英] Bash Loop Through Variable With Rows and Columns

查看:35
本文介绍了Bash通过具有行和列的变量循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的测试,测试了不同的解决方案之后,我还没有找到一个可行的解决方案.Bash和Shell脚本不是我最擅长的领域.

After searching around for many hours testing different solutions, I've yet to find a working solution. Bash and Shell Scripting is not my strongest area.

我有一个变量,其中包含行(新行)和列(制表符分隔).我想做的是遍历行并获取"Column X",然后将该项目放入变量中,以便我可以对该Cell进行操作.即例如回声.

I have a variable which has rows (new lines) and columns (tab separated). What I want to do is loop through the rows and get 'Column X' then put that item into a variable so I can do something with that Cell. i.e. echo out for example.

行数未知,但列数已知,因此例如可以始终获取列3.

There are an unknown number of rows, but known number of columns, so that it is possible to always get Column 3 for example.

我正在Linux上运行Bash v.4.

I'm running Bash v.4 on Linux.

示例输入(找不到在此处实际标签"内容的方法?);

Example Input (can't find a way to actually 'tab' things on here?);

UniqueID1 -tab- Description -tab- FriendlyNameABC
UniqueID1 -tab- Description -tab- FriendlyNameXYZ

示例输出将是遍历清单,获取FriendlyNameABC(第3列),然后将其回显.

Example output would be to loop through the listings, get FriendlyNameABC (col 3) and echo that out.

我正在尝试在此处扩展此脚本,

I am trying to expand this script here, https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77277&p=1178478#p1178478 - which is a shell script to connect to WiFi via WPS. The script provided on that page (below for reference) works, but it is reliant on selecting only the strongest WiFi signal, which isn't always going to be the network you want to connect to.

#!/bin/bash
# only run if there's no current wifi connection
if  ! ifconfig wlan0 | grep -q "inet addr:" ; then
       # grab AP's that support WPS, sort by strength and select the strongest 
        wpa_cli scan_results | grep WPS | sort -r -k3 | awk 'END{print $NF}'  >/tmp/wifi
        read ssid < /tmp/wifi
        wpa_cli wps-pbc $ssid
fi

当前,我已经设法修改了上面的脚本,因此以'wpa_cli ..'开头的行现在显示如下,这将整个已标识的WiFi网络置于一个变量中,但是随后我很难进行拆分这一切.

Currently, I've managed to modify the script above so that the line starting with 'wpa_cli..' is now reading as follows, which puts the entire identified WiFi networks in a variable, but then I'm struggling to split this all out.

wpa_cli scan_results | grep WPS | sort -r -k3 $allConnections

从本质上讲,我需要将上面的代码转换为该伪代码;

In essence, the above code I need translating into this pseudo code;

#!/bin/bash
# only run if there's no current wifi connection
if  ! ifconfig wlan0 | grep -q "inet addr:" ; then
       # grab AP's that support WPS, sort by strength and select the strongest 
        wpa_cli scan_results | grep WPS | sort -r -k3 | awk 'END{print $NF}'  >/tmp/wifi
        read ssid < /tmp/wifi

        #This is the section below that I'm struggling to implement (this is pseudo code, the only bit of working code is the line starting with wpa_cli...)
        while(ssid.hasNext(){
            #Try connecting to the network
            wpa_cli wps-pbc $ssid
            if(successful){break();}
        }
fi

我应该添加,我已经假设数据是制表符,并根据运行命令时的布局外观来换行;

I should add, I've assumed that the data is tab and new line delimited based on how the layout looks when I run the command;

wpa_cli scan_results

在Linux命令行上运行命令时,是否可以确认分隔符?

Is there a way to confirm the delimiter when running commands at the Linux command line?

推荐答案

如果针对每个单元格"的逻辑很简单,则可以执行以下操作:

If your "for each cell" logic is simple enough, you can do something like:

echo $VARIABLE | cut -f3 -d$'\t' | xargs echo

# echo $VARIABLE |       # pipe the value of VARIABLE to the next statement...
# cut -f3 -d$'\t' |      # take the 3rd column, using tabs for the delimiters
# xargs echo             # for each resulting row, call `echo` with that row as the argument

如果您需要做一些更复杂的事情:

If you need to do something more complicated:

for CELL in $(echo $VARIABLE | cut -f3 -d$'\t'); do     # for each line in the result of this command...
    echo $CELL
    # Other stuff with $CELL
done

这篇关于Bash通过具有行和列的变量循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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