您好,我想要一个简单的shell脚本,可以从文件的给定路径中查找文件的名称。像shell脚本从其路径中查找文件名
$path = "/var/www/html/test.php";
然后我想在某些变量中获得值“测试”。也只有.php文件present.I使用bash shell。 感谢
您好,我想要一个简单的shell脚本,可以从文件的给定路径中查找文件的名称。像shell脚本从其路径中查找文件名
$path = "/var/www/html/test.php";
然后我想在某些变量中获得值“测试”。也只有.php文件present.I使用bash shell。 感谢
尝试:
path="/var/www/html/test.php"
name=$(basename "$path" ".php")
echo "$name"
的报价只以防止有问题的时候$path
包含空格。
使用内置的UNIX命令:
basename "/var/www/html/test.php"
string="/var/www/html/test.php"
oIFS="$IFS"; IFS='/'
set -A str $string
IFS="$oIFS"
echo "strings count = ${#str[@]}"
len=${#str[@]}
pos=`expr $len - 1`
echo "file : ${str[$pos]}";
输出 -
strings count = 4
file : test.php
使用基本名()函数。它在UNIX中是buily功能
感谢您的支持! – 2014-11-10 00:18:05