Did you know you can test bash scripts line by line? Well,
The content of the bash script is:
#bash #sh #shell #scripting #debug #debugging
bash -x
is here to help:$ bash -x your_script.sh
+ a=10
+ echo 10
10
The content of the bash script is:
#!/bin/bash
a=10
echo $a
#bash #sh #shell #scripting #debug #debugging
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
I sometimes forgot to pull data from git before start working on a project. To minimize the headache of merge conflict or having to
If you have similar issues and you're solving it in a different manner, I'd be happy to hear about it. :)
#linux #mac #bash #script #git #cron #crontab #cronjob
reset head
and stash
your data you can set a cronjob to run every 10 minute or so. And inside of your bash script CD
into eaach of your folders and issue git pull
command.If you have similar issues and you're solving it in a different manner, I'd be happy to hear about it. :)
#linux #mac #bash #script #git #cron #crontab #cronjob
How to zero-pad a number in
In order to zero-pad a number you need to use do like below:
Here I have used
As simple as that.
#bash #printf #zeropad #zero-pad #zeropadding
bash
?printf
is here to help :)In order to zero-pad a number you need to use do like below:
your_number_var=1
output=$(printf "%02d" $your_number_var)
echo $output # 01
Here I have used
%02d
. the number 2 refers to numbers of padding and d
refers to digit
. So to zero-pad to 5 you can use %05d
.As simple as that.
#bash #printf #zeropad #zero-pad #zeropadding
If you forget to pull your projects from git in a regular interval and many users working on the same projects, then there is a solution for you!
Create a bash script file as follow and make it executable by
Now as a final step, put it in your crontab:
#linux #git #pull #cronjob #crontab #cron #bash
Create a bash script file as follow and make it executable by
chmod +x puller.sh
:puller.sh
file content:#!/bin/bash
echo 'Iterating over folders...'
for dir in *
do
test -d "$dir" && {
cd ${dir}
echo "git pull $dir"
git pull
cd ".."
} || {
echo "------> $dir is not a directory <-------"
}
done
NOTE:
this file should reside in your folder's project root. In my case it is in /Your/Projects/Folder
.Now as a final step, put it in your crontab:
10 * * * * bash -c "cd /Your/Projects/Folder; bash puller.sh >> /var/log/git_pull_output.log"
#linux #git #pull #cronjob #crontab #cron #bash
We have talked before about how to get current month using the below line of code:
It prints out 01, 02, etc.
As per the
So you can remove leading zero by hyphen as below:
It prints out 1, 2, etc.
#linux #bash #date
echo $(date +%m)
It prints out 01, 02, etc.
As per the
GNU date manpage
:By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field
So you can remove leading zero by hyphen as below:
echo $(date +%-m)
It prints out 1, 2, etc.
#linux #bash #date
In order to enable bash completion in Kubernetes you can usee the below command in linux bash:
Now to test this enter the below command and you should see the completion:
It should be expanded to
#linux #bash #shell #kubernetes #kubectl
source <(kubectl completion bash)
Now to test this enter the below command and you should see the completion:
kubectl cl<TAB>
It should be expanded to
kubectl cluster-info
.#linux #bash #shell #kubernetes #kubectl