SDカードリーダーのSDカードがどのデバイスに接続されているかを示すスクリプト

SDカードリーダーのSDカードがどのデバイスに接続されているかを示すスクリプト

SDカードリーダーがあります。さまざまなサイズのSDカードを接続できる3つの場所のシンプルなUSBデバイスです。

/dev/sdbSDカードを差し込むと、次のコマンドでSDカードを使用できることがわかります。

df -h

blkid -o list

fdisk -l

以下を自動的に実行するスクリプトを作成することは可能ですか?

  1. SDカードを使用できるデバイスブロックを教えてください(例:/dev/sdaまたは/dev/sdb...)
  2. SDカードをマウント解除します。

udevadm info -a -n /dev/sdb私のSDカードリーダーの独自のATTRS {idVendor}、ATTRS {idProduct}、およびATTRS {serial}の詳細を作成しました。

スクリプトはこれらの詳細を含むデバイスブロックをインポートできますか?

ベストアンサー1

数時間のインターネット検索の最後に見つかりました。この便利な答え

使用df -h後、blkid -o listSDカードがにあることがわかりました/dev/sdb。その後、udevadm info -a -n /dev/sdb私のSDカードリーダー(USBデバイス)のProductID、VendorID、およびシリアル番号を見つけるために使用しました。

それでは、SDカードリーダーを別のコンピューターに接続しているとしましょう。このスクリプトはすべてのデバイスブロックを繰り返し/dev/sdX、SDカードリーダーが接続されているデバイスブロックを報告します。

#!/bin/bash

# If ALL of these variables have values then you get "Success" below.
# If one or more of the variables do not contain a value (unset) or are null then you get "Failure" below.
# These are unique identifiers of the sd card reader
str_vendor="54jf"
str_product="775y"
str_serial="ID_SERIAL_SHORT=519S83946286"

for BLOCK in $(ls /dev | grep "^sd[a-z]$")
do
    echo "Device block " $BLOCK
    grep_vendor=$(udevadm info --query=all /dev/$BLOCK | grep $str_vendor)
    grep_product=$(udevadm info --query=all /dev/$BLOCK | grep $str_product)
    grep_serial=$(udevadm info --query=all /dev/$BLOCK | grep $str_serial)
    echo $grep_vendor
    echo $grep_product
    echo $grep_serial
    
    # From a comment to answer in above link... adding the colon [:] means to test if the variable is null OR unset
    # The udevadm commands result in the grep_* variables becoming NULL if the command returns nothing. (not sure?)
    # This is why the colon is needed. Note: Some reported that "This doesn't work in scripts where set -u is used"
    if [ -z ${grep_vendor:+x} ] || [ -z ${grep_product:+x} ] || [ -z ${grep_serial:+x} ]; then
        echo "Failure"
    else
        echo "Success"
    fi
    
done

変数の状態を確認する方法このスレッド

この機能が必要な理由は、単にスクリプトを実行してSDカードリーダーに接続されているすべてのSDカードをすばやく分割してフォーマットできるからです。

どんなフィードバックでも歓迎します!

乾杯、

おすすめ記事