スクリプト入力ファイル名または標準入力

スクリプト入力ファイル名または標準入力
#!/bin/sh


read vstup

if [ -f "$vstup" ]
    then
    cat $vstup
else if [ $vstup = "-"]
        then
         while [ $stadvstup != "q"]
            do
            read $stadvstup >> temp.txt
            done
        cat temp.txt
        rm temp.txt
fi
fi

ユーザーがファイル名または標準入力を入力できるスクリプトを作成したいと思います。ユーザがファイル名を入力すると、ファイルの内容が出力されます。 「-」を入力すると、ユーザーが入力した後に出力できます。次のコードを使用しました。誰かが私にヒントを与えることができますか?問題ありますか?

ベストアンサー1

このようなことをする必要があります。

#!/bin/sh

file="temp.txt"
read -r vstup

if [ -f "$vstup" ]
then
     cat "$vstup"
elif [ "$vstup" = "-" ]
then
     while read line
     do
         # break if the line is empty
         [ -z "$line" ] && break
              echo "$line" >> "$file"
     done
   cat $file
   rm $file
fi

おすすめ記事