shell 判斷文件是否是可執行文件


 

測試變量指定的文件是否存在且是可執行文件。如果存在且是可執行文件,則執行該文件,否則通過chmod命令賦予該文件可執行權限。

//test.sh

#!/bin/bash
echo "enter the name:"
read filename
if test -x $filename ; then
./$filename
else
sudo chmod +x $filename
ls -l $filename
fi

  //sss.sh

#!/bin/bash

echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
  break;
done
echo "You have selected $var"

 

運行

./test.sh

輸出:

enter the name:
sss.sh
-rwxrwxr-x 1 f f 0 Dec  7 06:59 sss.sh

 

再次運行

./test.sh

輸出

enter the name:
sss.sh
What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM