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
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列出我电脑上所有挂载的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
它可以是空的,也可以是多行。
当它是空的时候,我将呼应一个消息。"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
...我被卡住了。
#!/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