Is this even legal?

#!/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.

1 Like

Holy smokes! It is!

Man, BASH is a dirty nasty language :)

3 Likes

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

1 Like

Pretty much this - Just doing my part to automate myself out of a job, one script at a time.

I'm sure programmers love to find SysAdmins duct tape code all over the place.

1 Like

yep.

Bash is designed around that kind of stuff.
very useful in scripting to be able to pass stuff around so there is no user intervention.

1 Like

@Dje4321 BASH is looser than JS - and that language let's you get away with everything.

I LOVE IT!

bash can do some very cool stuff outside of scripts to

cat something1.txt | grep things | echo > something2.txt

that will read through something1.txt and output everyline that contains the string things and put it into something2.txt

1 Like

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.

3 Likes

my school security system uses bash to enfore policy's and manage logins and logouts.

easy to manupulate if you know how it works. shellshock is something bash needs to be patched with.

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.

That's how we roll. =)

1 Like

thats just piping man.

its just connecting standard in's with standard outs.

1 Like

You don't even need to pipe it,
grep things something.txt >> something2.txt
should do the same thing.

1 Like

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

cat something1.txt | sort something.txt | uniq -iu | grep things | echo > something2.txt

@cotton @Dje4321

I've got some books that I seriously need to go through. Here is a collection of some lovely free books on Bash:

1 Like

wont that just append to something2.txt? >> is to append, but if it doesn't exist shouldn't you just > ?

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.

1 Like

Also @dmj isn't lying ---- [ , yes.... [ - the left square bracket... it has a man page.

I'm not kidding try it yourself - it's called test.

@Miguel_Sensacion - thank you! I have some good reading material :)

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.

2 Likes

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