views:

80

answers:

7

I have the databases name in following format

username_databasename

Now I want to put separate database backups in username directory like

/backups/username/backup

How can I get the usernamae from that string

I also want that if string does not contain underscore (_) then the backup should go to

/backups/others/backup
+3  A: 

You can do:

username=others
if echo $name | grep '_'; then
 username=$(echo $name | cut -d'_' -f 1)
fi
codaddict
thanks for that . i also want that if (_) is not present then default username = others
Mirage
+2  A: 

You could use a variation on:

case $fullname in
(*_*) username=$(echo $fullname | sed 's/_.*//');;
(*)   username=others;;
esac
backupdir="/backups/$username/backup"
Jonathan Leffler
+2  A: 

Jonathan Leffler has a nice, clean answer.

If you don't want to use sed for some reason, you can replace the $(echo | sed) bit with:

username="${fullname%_*}"

which does the same thing.

Jander
A: 

you can use awk:

username = `echo $name | awk -F"_" '{print $1}'`
Vijay Sarathi
+1  A: 

No need for cut, grep, sed or awk here. Just use bash's inbuilt parameter substitution:

db_name=username_databasename

username=others
if [[ ${db_name} =~ '_' ]]; then
   username=${db_name%%_*} 
fi
backup_dir=/backups/${username}/backup

I prefer to stick to just one language per script, with as few forks as possible :-)

Johnsyweb
+1  A: 

For a bash-only solution with no potentially expensive calls to external programs (only important if you do it a lot, which may not be the case here):

pax> export x=a ; if [[ "${x%%_*}" != "${x}" ]]; then
...>    export bkpdir=/backups/${x%%_*}/backup
...> else
...>    export bkpdir=/backups/others/backup
...> fi
pax> echo "     ${bkpdir}"
     /backups/others/backup

pax> export x=a_b ; if [[ "${x%%_*}" != "${x}" ]]; then
...>    export bkpdir=/backups/${x%%_*}/backup
...> else
...>    export bkpdir=/backups/others/backup
...> fi
pax> echo "     ${bkpdir}"
     /backups/a/backup

The if statement detects if there's an underscore by checking a modified string against the original. If there is an underscore, they will be different.

The ${x%%_*} gives you the string up to the removal of the longest _* pattern (in other words, it deletes everything from the first underscore to the end).


A (slightly) simpler variant would be:

export bkpdir=/backups/others/backup
if [[ "${x%%_*}" != "${x}" ]]; then
    export bkpdir=/backups/${x%%_*}/backup
fi
paxdiablo
In which way is it bash-only? `%%` operation is defined in POSIX. Also, you don't need to put "export" everywhere. In fact, I don't see why would you want to put it at all, since you don't do any external program calls.
Roman Cheplyaka
I meant bash-only as in no external calls to grep or sed. I didn't mean it would only work in bash. And I generally use export where there's doubt since there's few things more annoying than trying to figure out why a started process can't see the environment variable. My use of export has nothing to do with my answer however - it's simple supporting code to give a full working sample.
paxdiablo
ha ha i think i need to learn so many things in order to understand ur answer. Can you please give me some tutorial to understand export , %% thing.i also don't know much seb ,awk. any tutts which can make me write program like u. Although i got my answer in simple steps but i love the way how things can be done
Mirage
paxdiablo
Thanks buddy but i have seen that experience really speaks
Mirage
A: 

In bash, you can use arrays, like so:

str="a/bc/def"
IFS="/" arr=($str)
echo ${arr[0]}; # prints 'a'
echo ${arr[1]}; # prints 'bc'
echo ${arr[2]}; # prints 'def'

echo ${arr[@]}; # prints 'a bc def'

If you want to split the string by a different "separator", just change IFS="/" line to that separator, eg

str="a,bc,def"
IFS="," arr=($str)
krico