我想寫一個簡單的bash腳本來顯示文件的內容。Bash腳本,'for'循環的用法
#!/bin/bash
echo 'Input the path of a file or directory...'
read File
if [ -e $File ] && [ -f $File ] && [ -r $File ]
then
echo 'Displaying the contents of the file '$File
cat $File
elif [ -d $File ] && [ -r $File ]
then
echo 'Displaying the contents of the directory '$File
for FILE in `ls -R $File`
do
cd $File/$FILE
echo 'Displaying the contents of the file '$FILE
cat $FILE
done
else
echo 'Oops... Cannot read file or directory !'
fi
用戶應輸入文件或目錄路徑。如果用戶輸入文件,則程序使用cat顯示該文件。如果用戶輸入一個目錄,它應該顯示所有文件的內容,包括子目錄中的文件。該計劃的這一部分工作不好。我想得到一個結果,不會顯示錯誤,如「沒有這樣的文件或目錄」,但只有文件的內容。你可以幫我嗎 ? 在此先感謝。
切勿在'$(command)'中使用'for i'。請參閱此答案以獲取更多詳細信息:http://stackoverflow.com/questions/19606864/ffmpeg-in-a-bash-pipe/19607361#19607361 否則,我認爲最好的解決方案是使用'find'來描述在這個線程的一些答案。 –