bashスクリプトから$ BASHPIDをコピーする方法は?

bashスクリプトから$ BASHPIDをコピーする方法は?

ネットワークネームスペースがあり、このnetns bash pidを使用してコマンドを実行したいと思います。 bashスクリプトでこれをどのように実行しますか?コード例:

sudo ip netns add ns1 #this is my network namespace

#In separate shell I have to run following commands
sudo ip netns exec ns1 bash
echo $BASHPID #I want to use this bash pid in the next command 

sudo iw phy phy0 set netns xxxx # xxxx is the example of bash pid

bashスクリプトでこれを実行できますか?コマンドを使用して別のシェルを開き、bash pidを変数として保存してこれを実行しようとすると機能しません。

ベストアンサー1

あなたの質問を完全に理解したかどうかはわかりません。最善を尽くしてお役に立てば幸いです。

  1. $BASHPID代わりにコードで使用しないのはなぜですか?xxx

    sudo iw phy phy0 set netns "$BASHPID"
    
  2. 変数に保存できます。

    YourVariable=$BASHPID
    

    次に、最初の変数で述べたのと同じ変数を使用します。

    sudo iw phy phy0 set netns "$YourVariable"
    
  3. ファイルに保存できます。

    echo "$BASHPID" > YourFile
    

    bash pidを含むYourFileという新しいファイルを作成します。

    後で読んで新しい変数に使用できます。

    YourVariable=$(cat YourFile)
    

    と同じディレクトリにあるか、またはYourFileを使用する必要がありますYourFile

    最後に、YourVariable項目2で述べたように使用することができます。

    sudo iw phy phy0 set netns "$YourVariable"
    

おすすめ記事