virt-install with cloud-init

2022-02-09 2 min read Learning Bash Virtualization Qemu Libvirtd

If you have not heard about cloud-init then you should definately search for it and learn about cloud-config as well. You can find the documentation for cloud-config here. It is used for configuring VMs when running in cloud environments. Specially useful to inject the ssh keys or set the root password but you can do anything in the cloud-config as there is a section where you can run bash commands. While this is useful for cloud instances, you can make use of this feature when running your vm with qemu or libvirtd. All you need to do is either attach a iso disc with user-data and meta-data files in it or even simpler, you can use virt-install command. The command that I use is as follows

Continue reading

Stay safe from phising

2022-02-05 2 min read Learning Bash Phising

Generally when you get a phising mail, the biggest challenge is finding if the site is genuine or not. And URL shortening services do not make it any easy. Earlier I had posted about using curl to expand the url in this post. However you might not be on a Linux terminal all the time (for example - you are checking something on your mobile), in that case what do you do?

Continue reading

Curl to exapnd short url

2022-01-09 1 min read Learning Bash

Many times I receive short urls in mail and other places like chats and messages. I first like to see the destination before I click on the url. I was looking for some way to do this in bash. I realized that this could be very simply done by looking at the Location header in the response from curl.

Example

1
curl -I https://bit.ly/32WwCp4|grep location

and the output should be something like

Continue reading

Sleep infinitely in bash

2021-12-19 1 min read Learning Bash

I am sure that you would have got a lot of instances where you have wanted to sleep for infinity and ended up doing this

1
2
3
4
while [[ 1 ]]
do
	sleep 3600
done

or some other such similar loop to sleep for some time and wrap it in infinite loop.

I learned something new recently and found it very useful. You can do the above with

1
sleep infinity

and you can use that even for a while loop

Continue reading

Ignore case when completing file names in bash

2021-12-11 1 min read Learning Bash

Sometimes you don’t want to have the bash completion work with case completion. There could be several reasons like one I dont like is the default xdg folders starting with capital letters.

So, if you know that bash uses readline for a lot of configuration then there is a very easy solution, you can just run this command and start a new bash shell :)

1
echo 'set completion-ignore-case on' >> ~/.inputrc

Enjoy 👍

Continue reading

Warn when battery is low

2020-08-30 2 min read Learning Bash Scripting

I am very lazy to look at battery status and want to be warned when the battery is low. This means that even if I am away from the laptop, I can be warned that laptop is about to poweroff due to battery low. So, what kind of alerts I can have, many -

  1. zenity message box
  2. email
  3. sms with twillio
  4. Telegram message and possibility is endless.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

LOW=35 ## This si the lowest percentage at which I want the alerts.
## Parse the acpi statue and decide if alert needs to be sent
per=$(/usr/bin/acpi -b | awk '{print $4}'|sed 's/%.*//') 

# source ~/bin/cron-scripts/sendsms.sh
if [[ $(/usr/bin/acpi |grep -c "Full\|Charging") > 0 ]]
then
    #This is when the AC is connected.
    exit 0
fi


if [[ ! -z $per && $per < $LOW ]]
then
    # sendsms "Battery is at $per%"
    export sub="Battery is at $per%"
    echo "Battery is running out : $per"|/usr/sbin/sendmail -t
    display_msg.sh "ACPI Warn" "Battery is at $per%"
    echo "ACPI Warn" "Battery is at $per%" | /home/amitag/bin/twilio-sms +919535210501
    zenity --warning --text="Battery is at $per%"
fi

Put this script somewhere and schedule in cron for every minute and enjoy.

Continue reading

Some good find alias.

2020-08-23 1 min read Learning Bash

Here are some interesting alias’s that you may want to add to your bashrc file or where-ever else you add your aliase’s. Very useful if you use find commonly.

There are four aliases defined here and have a comment explaining what it does. but these are so simple and useful that you probably dont even need the comments.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

# ff:  to find a file under the current directory
ff () { find . -name "$@" ; }
# ffs: to find a file whose name starts with a given string
ffs () { find . -name "$@"'*' ; }
# ffe: to find a file whose name ends with a given string
ffe () { find . -name '*'"$@" ; }
# very very useful function: for finding files with ignore case, just type "f <part of filename>"
# This in combination with alias for 'g' is deadly.
#
f () { find . -iname '*'"$@"'*' ; }

Hope this is useful for you.

Continue reading
Older posts Newer posts