#!/bin/bash
declare -a myArray=('list1' 'list2' 'list3' 'list4');
for i in "${myArray[@]}"
do
python SumScript.py $i #<-- is this even allowed?
#Passing $i as an argument to a python function
#from a bash script?
done
Just curious if this is legal, almost legal, barely legal or not allowed at all.
honestly not up enough on bash scripting to know if it would work, but i'd assume you meant more.. is it proper to do things like that in programming, in which the answer is usually a resounding no, usually frowned upon like goto, or using system in c etc.
so, depending on the example it can work, but is usually regarded as a hack as opposed to proper programing to write commands to other languages or calling external programs.
for me i'd say it can be useful as a duct-tape solution, for prototyping, quick fix etc. or if you aren't a programmer, don't have any interest at that time, but can make a simple script to increase productivity or whatever maybe
I've noticed that about bash, it does a whole lot with just a few keystrokes. I'm really starting to fall in love with it and perl. Both are terse as heck and just get stuff done.
In case you're serious, that's what KISS and Unix way is about: small programs and scripts, each doing one simple thing, passing arguments and piping output to each other. It may be frowned upon when you use external programs invocations inside your fancy python or perl scripts, because stuff like exec() is a bit wasteful and you're better off using some specific library directly.
But in shell scripts it's the only way you do anything. And really, depending on a shell, some stuff in your example may be an internal shell command or an actual application: 'declare' - it's an internal Bash command, but it also exists as a standalone executable in some systems - and the same applies to 'for'. If you see ' [ some__expression ] ' in a shell script, its actually a '[' program (which is why spaces before and after '[' and ']' are important) which uses some__expression and ']' as arguments and passes said expression to a 'test' program, which, again, may be a standalone executable or a shell buiilt-in. You may see stuff like '. something.sh' and this dot there in the beginning is actually an alias to a 'source' command - and again, it may be a shell built-in or a standalone executable.
remewmber that just like your program, sed, awk, uniq, sort, grep,are all programs too. Heck, you could make your own combinations of programs into 1 program per-say
Either would work, pretty sure that just using > would be fine, I was thinking that multiple lines would overwrite the file so you'd only end up with the last line but it should just redirect the whole output.
just remember, >> is different than |. The former redirects the full output once its done, while the later directly redirects output as it happens. Piping is useful to conserve on resources and sometimes the only way to get something done when there is not enough space.
For example, I once read an article where someone was backing up some 500GB or so, and his drive was full. So obviously he couldn't tar everything and then send the copy. His solution, pipe the tar into a network storage. So as something got tar'd it was immediately piped to the network location. Pretty slick if you ask me.
Everything that works to get you're goal is Legal in Bash..... Just watch out with the fallout and don't run it as root if you're not sure result is what you want :p