Our new Indie Games subforum is now open for business in G&T. Go and check it out, you might land a code for a free game. If you're developing an indie game and want to post about it, follow these directions. If you don't, he'll break your legs! Hahaha! Seriously though.
Our rules have been updated and given their own forum. Go and look at them! They are nice, and there may be new ones that you didn't know about! Hooray for rules! Hooray for The System! Hooray for Conforming!

Help with a shell script

DelzhandDelzhand motivatedbattle programmerRegistered User regular
edited August 2010 in Help / Advice Forum
I'm trying to pare down a string from something like this:

Database name : mysite_drupal

to just plain

mysite_drupal

What I have so far:
sitename=$(drush status | grep "Database name")
sitename=${sitename##*:} //this is supposed to get rid of all characters until the last instance of :
echo xxx $sitename xxx

The xxx strings are there so I can see what's being output where, and here's the kicker, the output looks like this:

xxxmysite_drupal

It is ignoring the first space and completely truncating the xxx after the sitename, and I don't know why.

Delzhand on
9KKPPQw.png

Posts

  • rfaliasrfalias Registered User
    edited August 2010
    Delzhand wrote: »
    I'm trying to pare down a string from something like this:

    Database name : mysite_drupal

    to just plain

    mysite_drupal

    What I have so far:
    sitename=$(drush status | grep "Database name")
    sitename=${sitename##*:} //this is supposed to get rid of all characters until the last instance of :
    echo xxx $sitename xxx
    

    The xxx strings are there so I can see what's being output where, and here's the kicker, the output looks like this:

    xxxmysite_drupal

    It is ignoring the first space and completely truncating the xxx after the sitename, and I don't know why.

    Does that grep return that string like you typed?
    I tried it and it works for me if I just use a string as initial sitename. Only thing I can think of is that grep isn't returning what you are expecting.

    1036440-1.png
  • DelzhandDelzhand motivated battle programmerRegistered User regular
    edited August 2010
    I think grep is appending a newline or something. Is there an easy way to force it to print gremlins like \n?

    Edit: Hmm.
    tar -czf $sitename.tar.gz .
    gives me this:

    tar: \r.tar.gz: Cannot stat: No such file or directory
    tar: Error exit delayed from previous errors

    and ends up creating sitename_drupal just fine, minus the .tar.gz. Maybe I can hack it by renaming the file afterwards.

    9KKPPQw.png
  • mightyjongyomightyjongyo Registered User regular
    edited August 2010
    Use awk. You can have it split on spaces and then print the last field (assuming that "Database name : mysite_drupal" is the whole string).

    eg: echo $var | awk F ' ' '{print $NF }'

    (i'm not sure on the syntax, but you can google 'awk print last field')

    edit: I'm not sure if this is bash or what, but awk is a linux command so if you have some way to run it from within the language you're using it should still be fine.

  • DelzhandDelzhand motivated battle programmerRegistered User regular
    edited August 2010
    New question! If I want to rename a file to have a date in it, how would I do that? I want to rename myfile.tar.gz to myfile_08_18_10.tar.gz.

    In a script obviously, I can't just do "mv myfile.tar.gz myfile_08_18_10.tar.gz" of course.

    edit: disregard, I found the answer.

    9KKPPQw.png
Sign In or Register to comment.