I am struggeling a bit. As I write a ksh script I need to extract a Substring of a String where the number of occurances of the dilimiter is flexible.
This is, as my String holds the name of a file which might be compressed several times, therefore having more than 1 point (.). These points would be my delimiter, but as the supplier might include version numbers into the name of the file (e.g. software-v.3.2.4.tar.gz), I find no was to cut off the last suffix.
The progress is as follows:
Filename is saved in variable. File is decompressed first time. (taking the .gz suffix away of the file) Now I need to extract the .tar archive. But my command would still be holding the .gz suffix. Command would not work as the file has the suffix no more.
How do I get the suffix of my variable. I can not guarantee that the numbers of delimiters stay the same.
I tried several combinations of | rev | cur -d'.' | rev, but in this case I only get the suffix.
I aswell tried initialize the $fileName variable again with the actual name of the file, but therefore I would need to search the whole directory. I try to avoid that.
...
fileName="whole file name"
pathTo="path in which it should be moved after decompression"
if [ "$fileType" = "gz" ]; then
gzip $pathTo$fileName -d $pathTo
#Problem occurs right here
tar xfv $pathTo$fileName -C $pathTo
else
echo "Unknown Filetype. Cannot decompress. Script stopped."
exit
fi
...
I am thankful for any help. Greets
Yonkske