LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script: to truncate filename (http://www.linuxquestions.org/questions/linux-software-2/bash-script-to-truncate-filename-876437/)

casualzone 04-21-2011 11:51 PM

bash script: to truncate filename
 
try to write a simple bash script

If the input $1 is a "myfilename.txt",
How could I store the "myfilename" as my another variable instead of "myfilename.txt". I want the script to make a new directory name as myfilename

anomie 04-22-2011 12:01 AM

I believe there is bash(1)-fu to solve this sort of thing, but I'm pretty fond of g/awk(1):
Code:

_name=$(echo ${1} | awk -F'.' '{print $1}')

crts 04-22-2011 06:25 AM

Here is a pure bash solution:
Code:

dir_name=${1%.*}
If your name has several dots in it like
my.file.name.txt

then it will only delete the last part like
my.file.name

If you want the output to be just
my

then you can use
Code:

dir_name=${1%%.*}

casualzone 05-24-2011 10:01 PM

How about for the csh case?


All times are GMT -5. The time now is 03:14 AM.