How to get "case" to overwrite preset variables in a bash script
Problem description: I'm making a backup script for bash that will run without options in the command line. It's then suppose to use the preset variables in the script. I also want to be able to input options that will be used instead of the preset variables.
The problem is that the way I'm doing it doesn't work properly, the preset variables doesn't get overwritten with the values i put in at the command line. Does anyone have an idea what I'm doing wrong or suggestions for better ways to do this? Thank you in advance for taking the time to look at this and help out. Code:
#!/bin/bash |
Seems to work fine for me:
Code:
#!/bin/bash Code:
~> ./testvars.sh -dd=/tmp/tmp |
Thank you for testing that for me.
While I was waiting for an answer I think I found the solution to my problem. In my original script i have several variables that depend on one variable so when i update the value of one, i need to update all the others that has used that variable? Meaning if I do this Code:
VAR1=/tmp So in order for this to work i would have to exchange this Code:
-dd=* | --dest_dir=* ) VAR1=`echo $1 | sed 's/[-a-z_]*=//'`; shift;; Code:
-dd=* | --dest_dir=* ) VAR1=`echo $1 | sed 's/[-a-z_]*=//'`; VAR2=$VAR1/$USER; shift;; |
Hello Mogget :)
Glad you got it sorted. You might like to look at getopts for parsing the command line -- it's easier than using homegrown code, more transparent to read (and hence maintain) and runs quicker than subshell spawning echos and seds (not that they're a big deal). That said, your programming style is nice -- very easy to read and understand. Here's a sample getopts usage, from a backup script. The usage function is listed first to show what the getopts code is implementing Code:
#-------------------------- Charles |
Thank you very much for the compliment. I will have to look into this as soon as im done with the code i'm working on now.
The code seems a little bit over my head, but i think i will understand it after some reading, thinking and maybe some skimming of man files. Again thank you for giving me some feedback. I have been working alot on this lately and i think i soon have a script that is pretty versatile no matter the machine. |
All times are GMT -5. The time now is 03:11 AM. |