How to autodetect device name in Linux

1

I have an encrypted USB dongue with cryptosetup, to mount it I have to execute a script that has hardcoded (or admits it as a parameter) the device name of that dongue that previously you had to search.

I wonder is there is any way via bash scripting or even doing a miniprogram to make an autodetection of the device name, leaving the workflow something like this:

  • Connect USB dongue
  • Execute script
  • Script searches for the device name of the dongue and mounts the device
  • Cryptosetup asks for the password
  • Device is mounted

Oscar Carballal

Posted 2013-03-07T11:51:33.730

Reputation: 133

Answers

1

The device name should appear in dmesg. Try something like this:

dev_name="/dev/"`dmesg | perl -ne '/.+\[(.+?)\] Attached/ && do{print "$1\n"}' | tail -n 1`

Assuming that your USB dongle is the last device attached (hence the tail -n1), this should save the device name as $dev_name.

terdon

Posted 2013-03-07T11:51:33.730

Reputation: 45 216