Amostra bash finalizada
Existe uma pequena parte de um script que escrevi para criar e instalar chave usb ao vivo , (dual boot ubuntu - debian):
A primeira parte USBKEYS=...
é a resposta para essa pergunta
Em suma, isso:
list removable devices, driven by sd
and having non zero size.
Nota Este script usa dialog
, que parece não estar instalado por padrão no Ubuntu. Mas dialog
pode ser substituído por gdialog
, whiptail
ou 'zenity or even
easybashbui'.
#!/bin/bash
export USBKEYS=($(
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/ |
xargs grep -Hv ^0$ |
cut -d / -f 4
))
export STICK
case ${#USBKEYS[@]} in
0 ) echo No USB Stick found; exit 0 ;;
1 ) STICK=$USBKEYS ;;
* )
STICK=$(
bash -c "$(
echo -n dialog --menu \
\"Choose wich USB stick have to be installed\" 22 76 17;
for dev in ${USBKEYS[@]} ;do
echo -n \ $dev \"$(
sed -e s/\ *$//g </sys/block/$dev/device/model
)\" ;
done
)" 2>&1 >/dev/tty
)
;;
esac
[ "$STICK" ] || exit 0
echo $STICK...
Amostra (acabei de conectar 3 pen drives USB - além dos meus 3 discos rígidos):
Substituindodialog
porgdialog
(nalinha24),dê:
Mas a sintaxe pode ser usada com outro utilitário diálogo , como whiptail
...
Parte essencial
export USBKEYS=($(
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/ |
xargs grep -Hv ^0$ |
cut -d / -f 4
))
for dev in ${USBKEYS[@]} ;do
echo $dev \"$(
sed -e s/\ *$//g </sys/block/$dev/device/model
)\" ;
done
sdd "Storage Media"
sde "Freecom Databar"
sdf "silicon-power"
Explicação detalhada
Isso usa alguns bashisms :
export USBKEYS=($( # Declaration of *array* 'USBKEYS'
grep -Hv ^0$ /sys/block/*/removable | # search for *not 0* in 'removable' flag of all devices
sed s/removable:.*$/device\/uevent/ | # replace 'removable' by 'device/uevent' on each line of previous answer
xargs grep -H ^DRIVER=sd | # search for devices drived by 'SD'
sed s/device.uevent.*$/size/ | # replace 'device/uevent' by 'size'
xargs grep -Hv ^0$ | # search for devices having NOT 0 size
cut -d / -f 4 # return only 4th part '/' separated
))
for dev in ${USBKEYS[@]} ;do # for each devices in USBKEY...
echo $dev \"$(r # echo device name and content of model file
sed -e s/\ *$//g </sys/block/$dev/device/model
)\" ;
done
Depois de inserir três pen drives na minha mesa:
grep -H . /sys/block/*/removable
/sys/block/loop0/removable:0
/sys/block/loop1/removable:0
...
/sys/block/sdc/removable:0
/sys/block/sdd/removable:1
/sys/block/sde/removable:1
/sys/block/sdf/removable:1
/sys/block/sr0/removable:1
(Sim, minha mesa possui 3 discos rígidos físicos: sda
, sdb
e sdc
. Primeiro, removíveis se tornam sdd
e sde
).
Então:
grep -Hv ^0$ /sys/block/*/removable
/sys/block/sdd/removable:1
/sys/block/sde/removable:1
/sys/block/sdf/removable:1
/sys/block/sr0/removable:1
Eu tenho a lista de dispositivos removíveis ,
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/
/sys/block/sdd/device/uevent
/sys/block/sde/device/uevent
/sys/block/sdf/device/uevent
/sys/block/sr0/device/uevent
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd
/sys/block/sdd/device/uevent:DRIVER=sd
/sys/block/sde/device/uevent:DRIVER=sd
/sys/block/sdf/device/uevent:DRIVER=sd
Eu tenho a lista de dispositivos removíveis que são acionados pelo driver sd
(ou seja, não sr
, nem floppy
)
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/
/sys/block/sdd/size
/sys/block/sde/size
/sys/block/sdf/size
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/ |
xargs grep -Hv ^0$
/sys/block/sdd/size:15224832
/sys/block/sde/size:7834944
/sys/block/sdf/size:7831552
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/ |
xargs grep -Hv ^0$ |
cut -d / -f 4
sdd
sde
sdf
Além disso:
export USBKEYS=($(
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/ |
xargs grep -Hv ^0$ |
cut -d / -f 4
))
set | grep ^USBKEYS=
USBKEYS=([0]="sdd" [1]="sde" [2]="sdf")
E finalmente:
cat /sys/block/$USBKEYS/device/model
Storage Media
cat /sys/block/${USBKEYS[2]}/device/model
silicon-power
mas
printf "|%s|\n" "$(</sys/block/$USBKEYS/device/model)"
|Storage Media |
É porque eu escrevi:
echo ${USBKEYS[2]} \"$(sed -e s/\ *$//g </sys/block/${USBKEYS[2]}/device/model)\"
sde "silicon-power"
Encolhido - jogado no golfe:
Existe uma versão abreviada
US=($(cut -d/ -f4 <(grep -vl ^0$ $(sed s@device/.*@size@ <(grep -l ^DRIVER=sd $(
sed s+/rem.*$+/dev*/ue*+ <(grep -Hv ^0$ /sys/block/*/removable)) <(:))) <(:))))
(Nota: <(:)
do pseudo arquivo vazio por garfo para :
isto é menor que /dev/null
mas não é realmente equivalente)
Duas linhas e uma variável UsbSticks
mantendo:
set | grep ^US=
US=([0]="sde" [1]="sdf" [2]="sdg")
Então, o script (começo do meu) pode se tornar:
#/bin/bash
US=($(cut -d/ -f4 <(grep -vl ^0$ $(sed s@device/.*@size@ <(grep -l ^DRIVER=sd $(
sed s+/rem.*$+/dev*/ue*+ <(grep -Hv ^0$ /sys/block/*/removable)) <(:))) <(:))))
case ${#US[@]} in 0)echo "No USB stick found.";exit 0;;1)S=$US;;*)S=$(sh -c "$(
sed -nre 's@/sys/block/(.*)/device/model:(.*)$@ ""@;H;${x;s/\n/ /g;
s/^/whiptail --menu "Choose an USB stick" 22 76 14/;p}' <(grep -H . $(
printf /sys/block/%s/device/model\n ${US[@]})))" 2>&1 >/dev/tty) ;; esac
whiptail --defaultno --yesno "Could I destroy content of $S!?" 10 70 6 || exit 0