เนื่องจากคิดไม่ออกแล้วว่าจะเอาคำสั่งอะไรดีที่จะ extract ชื่อ file จาก url ก็เลยทำดังนี้
url="http://www.somesite.org/some/path/here/and/the/filename.txt"
s1=`echo $url | sed -e 's|/| |g'`
n=`echo $s1 | wc -w `
fname=`echo $s1 | cut -d" " -f $n`
คำสั่ง sed
-e แปลว่า ให้ใช้ script ต่อไปนี้
s|/| |g แปลว่า แทนที่เครื่องหมาย / ด้วย ช่องว่าง
ส่วนตัว g แปลว่า global (ทั้งหมด)
ดังนั้นค่า s1 จะได้เป็น
http: www.somesite.org some path here and the filename.txt
คำสั่ง wc
wc -w แปลว่า นับจำนวนคำทั้งหมด
จากคำสั่งนี้จะได้ค่า 8
คำสั่ง cut
cut -d" " -f $n
-d แปลว่า
ตัวแบ่งคือ " " (ช่องว่าง)
-f แปลว่า
field ที่เท่าไหร่ ในที่นี้คือเอาตัวที่ 8
ดังนั้น fname จึงได้เป็น filename.txt
เอวังด้วยประการฉะนี้