grep

问题描述 投票:0回答:1

awk '{print $3}' lists all mounted USB Storages on my computer. It could be empty or multiple lines. ...

Here's something that work:mount | grep /media | awk '{print $3}'Can you provide a preview of your output so I can format it as well

  • If you really wanted the list items to be numbered in the

    1. fashion, you could do something like this.
    2. The if statement at the end triggers if loop never runs because of zero results.

From your description I understand that you like to dynamically create an menu or selection list from a command output given. A possible solution can be something like

Thanks to

Reading output of a command into an array in Bash
linux bash shell
1个回答
1
投票

列出我电脑上所有挂载的USB存储设备.

#!/bin/sh

output=$(mount|grep /media|awk '{print $3}')

if [[ -z $output ]]; then
    # output is empty
    echo "Empty not found"
else
    # output is not empty
    echo $output
fi

它可以是空的,也可以是多行。

当它是空的时候,我将呼应一个消息。

1
投票

"AAA #)BBB

#!/bin/bash

count=0

for each in $(mount | grep '/media' | awk '{print $3}'); do 
  count=$(( count + 1 ))
  echo "${count}) $each"
done

if [ "$count" -eq 0 ]; then
  echo "No disks found."
fi

CCC

...

0
投票

我被卡住了。

#!/bin/bash

# Gather the USB drives into an array
unset DRIVES
while IFS= read -r LINE; do
  DRIVES+=("${LINE}")
done < <(mount -l | grep /dev/sd | awk '{print $3}') # < You may change this for your needs ... since I dont have USB in my VM

# Iterate over an array to create select menu
select USB in "${DRIVES[@]}" "Quit"; do
  case ${USB} in
    /)
      echo "Drive ${USB} selected"
      # Further processing
      ;;
    "Quit")
      echo "Exit"
      break
      ;;
    *)
      echo "Not available"
      ;;
  esac
done

© www.soinside.com 2019 - 2024. All rights reserved.