Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You need to do three things with your script.
    1. Unless you're running on a highly unusual system, the very first line is incorrect:
      Code:
      #!/bash/sh
      Try running one of these commands at the shell prompt:
      Code:
      which bash
      which sh
      to figure out what to put after the #!.
    2. 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.
    3. 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
      Code:
      goal_2008-10-31_00-12-48
      and take the output from that command and assign it to the variable totalfilename?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by packet View Post
    below is my piece of code
    This is your code, is it? Well, since you are putting the "-" there in the first place, I would suggest that you simply stop doing that.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i think he means this
    Code:
    totalfilename="`$MC-$vardate-$vartime`"
    it can be this in the first place
    Code:
    totalfilename="`$MC_$vardate_$vartime`"

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  7. #7
    Just 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

  8. #8
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by wje_lf View Post
    burschik, I don't quite understand your response. Removing the hyphens from his script is not going to get the underscores to work.
    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'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...