An interesting scenario in
If you have list of numbers like below:
Now let's say you want to check a partial number like
would you do?
It's clear that you cannot do something like:
So to check all the list element and also check the partial number we can do as below:
We have explained
#python #any #for
Python
If you have list of numbers like below:
numbers= ["+98121434", "+9821322", "+986241234"]
Now let's say you want to check a partial number like
121434
, or 98121434
or +98121434
to see if it is inside of the list. Whatwould you do?
It's clear that you cannot do something like:
# YOU CANNOT DO THIS!
if number in numbers:
pass
So to check all the list element and also check the partial number we can do as below:
if any(partial_number in s for s in numbers):
pass
We have explained
any
before enough. Check previous posts for any
and all
.#python #any #for
Array and loop in bash script
To define an array you can use a structure like below, be careful that we don't use comma in between:
dbs=( 'test1' 'test2' 'test3' )
Now to loop over the array elements use
for
:for your_db in ${dbs[@]}
do
echo $your_db
done
This is it!
#bash #scripting #for #loop #array
How to prepend a string to all file names in a directory in a bash script?
The above one-line will loop over all files in current directory with
So for example a file with name
#python #bash #script #prepend #move #rename #for
for f in *.py; do mv "$f" "old-$f"; done
The above one-line will loop over all files in current directory with
.py
extension and prepend old-
into the files.So for example a file with name
main.py
will be renamed to old-main.py
#python #bash #script #prepend #move #rename #for