(patch for Bash) adding Python features to Bash
Padraig at Linux.ie
Padraig at Linux.ie
Thu Feb 13 05:27:18 EST 2003
William Park wrote:
> To Python and Bash users:
>
> I got tired of going back and forth between Python and Bash. So, I
> added few Python features to the shell.
A laudable goal.
> Patch for Bash-2.05b is
> included at the end. Use at your risk, and enjoy. :-)
>
> I hope people won't get mad at me for posting to <comp.lang.python>.
> But, this may be of interest to other Python/Bash programmers, like
> myself.
>
>
> 1. Integer sequence generator:
>
> {5..10} --> 5 6 7 8 9 10
> {05..10} --> 05 06 07 08 09 10
> {1..n} --> 1 2 3 4 (if n=4)
> {1..!m} --> 1 2 3 (if m='i' and i='3')
Hmm what has this to do with python?
Isn't range() the pythonic way?
Also note gcc C extension syntax for this is {5 ... 10} not {5..10}
> 2. Multiple variables in for-loop:
>
> for a,b,c in ... ; do
> echo $a $b $c
> done
>
> Normally, only one variable can be used. But, you can now follow
> Python's for-loop syntax,
> for (a,b,c) in [ (...), (...), ... ]:
> print a, b, c
> as long as the variables are separated by ',' (comma) without any
> spaces. Their functions are identical, if you serialize the list
> elements.
Isn't the bash way to do this like:
while read a b c; do
echo $a $b $c
done
> 3. Python's map() function:
>
> arraymap command a b c ...
>
> 4. Python's filter() function:
>
> arrayfilter filter name
>
> 5. Various list/dict operation in Python:
>
> array [-kvlrs] [-i value] [-j sep] name [arg...]
>
> By default, print array indexes and values, separated by '\t',
> mimicking dict.items() in Python. Only one option is allowed, so
> the last one takes effect.
> -k Print only indexes. --> dict.keys()
> -v Print only values. --> dict.values()
> -l Print size of each array element. --> [ len(v) for v in list ]
> -i value Print all indexes that have 'value'. --> list.index(value), repeat...
> -j sep Print all values with 'sep' separator. --> sep.join()
> The following operation changes the array in-place.
> -r Reverse the array, keeping the same indexes. --> list.reverse()
> -s Sort the array, keeping the same indexes. --> list.sort()
> -c Collapse the array, so that there is no missing index.
>
> If one or more arguments are present, then append them sequentially to the
> end of array, mimicking list.append(arg) or list.extend([arg,...]) in
> Python. It doesn't create new array, so create it manually.
> -j sep Split each 'arg' string by 'sep' separator, and append each
> segment to the end of array. If 'sep' is null, then no
> splitting is done. --> list.extend(arg.split(sep)), repeat...
>
More information about the Python-list
mailing list