Results 1 to 8 of 8
Hello All,
How to change from '-' to '_' in a file name below is my piece of code
Code:
#!/bash/sh
MC=goal
vardate=`date +%F`
vartime=`date +%H-%M-%S`
totalfilename="`$MC-$vardate-$vartime`"
echo "my file ...
- 10-31-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 45
how to change from '-' to '_' (underscore)
Hello All,
How to change from '-' to '_' in a file name below is my piece of code
Code:#!/bash/sh MC=goal vardate=`date +%F` vartime=`date +%H-%M-%S` totalfilename="`$MC-$vardate-$vartime`" echo "my file name is = $totalfilename"
the output is = goal-2008-10-31-11-22-32
I need the following output format
goal_2008-10-31_11-22-32
I want '_' underscore to separate only 3 fields
I tried to put '_' underscore after $MC and $vardate and $vartime
but it is not given expected output.
Help me out
Thanks in advance
- 10-31-2008 #2
You need to do three things with your script.
- Unless you're running on a highly unusual system, the very first line is incorrect:
Try running one of these commands at the shell prompt:Code:#!/bash/sh
to figure out what to put after the #!.Code:which bash which sh
- It is permissible to use an underscore character as part of a variable name. So when you refer to $MC_, bash will look for a variable named MC_, which you haven't defined. Instead, use ${MC}_ to separate your intended variable name from the following underscore.
- When you assign values to variables vardate and vartime, you correctly use the backtick (`) to take an expression, use it as a command, and take the result of that command and use it in your command line. In both of these cases, that command is the date command.
But then you also use the backtick in your assignment to the variable totalfilename. Why is this? Do you really want to run a command like
and take the output from that command and assign it to the variable totalfilename?Code:goal_2008-10-31_00-12-48
--
Bill
Old age and treachery will overcome youth and skill.
- Unless you're running on a highly unusual system, the very first line is incorrect:
- 10-31-2008 #3Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
- 10-31-2008 #4
burschik, I don't quite understand your response. Removing the hyphens from his script is not going to get the underscores to work.
--
Bill
Old age and treachery will overcome youth and skill.
- 11-01-2008 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
i think he means this
it can be this in the first placeCode:totalfilename="`$MC-$vardate-$vartime`"
Code:totalfilename="`$MC_$vardate_$vartime`"
- 11-01-2008 #6
No, it can't, for reason number 2 and reason number 3 in my previous post.
--
Bill
Old age and treachery will overcome youth and skill.
- 11-03-2008 #7Just Joined!
- Join Date
- Feb 2008
- Posts
- 45
solved
Hello All ,
I done a mistake in the code
totalfilename="`$MC-$vardate-$vartime`"
this as to be
totalfilename="$MC-$vardate-$vartime"
and the solution given by wje_lf is correct that is I need to { } to MC i.e the following line is correct that what i need
totalfilename="${MC}_${vardate}_${vartime}"
the output will come as
goal_2008-10-31_00-12-48
thanks lot
- 11-03-2008 #8Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Well, no, it wouldn't. I did not realize the original poster does not understand variable naming. The bash syntax for parameter expansion is ${var}. One should not use shortcuts unless one understands how they work. In light of this new information, my reply would be:
Code:date +'goal_%F_%H-%M-%S'


Reply With Quote
