21 Oct 2007

Proste operacje na stringach w Bashu.

By mieć to zawsze pod ręką i się nie zastanawiać czy na pewno jest dobrze :)
#!/bin/bash
PATH2=/usr/src/linux
FILE=archive.tar.gz
PATHFILE=/home/johny/Desktop/ff.zip
echo '## THE PATH'
echo $PATH2 '=>'  ${PATH2%/*}             # rm last / or get parent
echo '## FILE'
echo ${FILE} '=>' ${FILE%%.*}           # name without extension
echo ${FILE} '=>' ${FILE##*.}           # only last extesion
echo ${FILE} '=>' ${FILE#*.}            # full extension
echo '## PATHFILE'
echo $PATHFILE '=>' ${PATHFILE%/*}
echo ${PATHFILE} '=>' ${PATHFILE##*.}
echo ${PATHFILE} '=>' ${PATHFILE#*.}

Wynik:
## THE PATH
/usr/src/linux => /usr/src
## FILE
archive.tar.gz => archive
archive.tar.gz => gz
archive.tar.gz => tar.gz
## PATHFILE
/home/johny/Desktop/ff.zip => /home/johny/Desktop
/home/johny/Desktop/ff.zip => zip
/home/johny/Desktop/ff.zip => zip

No comments:

Post a Comment