BASH array question
Below is the bash -xv output of a section of code that i seem to have messed up.
Code:
copysys Code:
### Function to copy system files to $HOME Thanks in advance for the help. |
It looks like the "$" in front of the >/dev/null is what's messing things up. I'm not sure what you're trying to accomplish there, maybe you meant to use a "&"?
|
hmm my reply thanking you disappeared... You are correct i wanted the &, not the $. a 2nd pair of eyes is often a great help.
|
That's not the only problem with this code.
Code:
So the cp command is actually executed as this: Code:
cp /etc/hosts /etc/exports ... etc ... ${HOMEDIR} The proper array-setting syntax is "array=( <wordlist> )". Be sure to put quotes around any string that has spaces or globbing patterns it too (such as your "/etc/sysconfig/network-scripts-ifcfg-eth*" here, unless you actually want it to expand into the full list of files first. That may or may not be what you want). It might also be a good idea to declare the array local to the function, unless you need to use it elsewhere in the script. (But then again, if you did, you'd probably set it outside the function anyway, right?) Code:
echo "Copy system files to user directory." >> ${logname} This is a vital concept in scripting, so read, and reread until you fully understand, these links: http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes Finally, a suggestion about style. Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands (meaning the function contents and the for loop in this case), and clearly separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be so a year or so down the line). Many people also think that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block. In addition, environment variables are generally upper-case, so while not critical, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them. And using the full bracket syntax when you don't need it does nothing but clutter up the script. I recommend leaving them off. With all this taken into account, I think the above function should probably look more like this: Code:
Code:
cp -t "$homedir" "${sysfiles[@]}" Actually #2, it's not usually good form to store data inside a script. It would be better to import the list values from a separate file or the command line. this would allow you to update the list as necessary without worrying about editing the script itself, and possibly breaking something in it. Or at the very least put all of your user settings at the top of the script, so you don't have to hunt through the code to change things. |
All times are GMT -5. The time now is 01:44 PM. |