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
精彩评论