开发者

shell script to find file name from its path

Hello i want a simple shell script that fi开发者_运维技巧nd the name of the file from a given path of the file. like

$path = "/var/www/html/test.php";

then i want to get value "test" in some variable. Also only .php files are present.I am using bash shell. Thanks


Try:

path="/var/www/html/test.php"
name=$(basename "$path" ".php")
echo "$name"

The quotes are only there to prevent problems when $path contains spaces.


Use the built in UNIX command:

basename "/var/www/html/test.php"


Use the basename() function. It is buily in UNIX function


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]}";

Output-

 strings count = 4
 file : test.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜