Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
In Linux we have a command called test, you can check whether a file/directory exists or not and run commands based on the result. For example let's say we want to check if a folder exists and if it does not exist, create the folder.

For checking directory existence we use test -d and for file existence we use test -f, so for our example in order to check if the directory exists we use test -d and in case the folder does not exists we will create it:

directory_to_check="/data/mysql"
test -d $directory_to_check || {
echo "$directory_to_check does not exist, creating the folder..." && mkdir -p $directory_to_check || {
echo "$directory_to_check directory could not be created!"
exit 1
}
}

NOTE: you can read more about exit codes with hashtag #exit_code

#bash #linux #directory_existence #file_existence
If you have multiple statements and you want to time the whole statements in a bash script you can use time command, but you don't need to use inside of the script. If you have an script called my_long_tasks.sh you need to just:

$ time my_long_tasks.sh
real 0m20.894s
user 0m3.664s
sys 0m0.452s


That's it. In case you want to just time part of your statements in your bash script then you need to:

start=`date +%s`
stuff
end=`date +%s`

runtime=$((end-start))

#linux #bash #time
In bash script you can get count of given arguments to your script using $#. If you want to get a positional argument use $ and then number like $1. If you want to get the exit code of the previous command use $?.

#linux #bash #script
Did you know you can test bash scripts line by line? Well, 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?

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 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 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 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:

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:

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
How to recursively rename filenames?

find . -name '*txt' -exec bash -c ' mv $0 ${0/brand-/category-}' {} \;


The above command renames txt files starting with brand- to category-.

#linux #bash #find #rename #batch_rename