Hello world

1. This command recursively finds given text in files. The main advantage of using find instead of simply execute ‘grep *’ is the fact that given command writes names of the files the text was found in:

find ./ -exec grep -Hn "your phrase" {} \;

2. My favourite loop type in bash is the while loop. It is a really universal construction that allows you parse files, join outputs of commands, etc. The only disadvantage of this loop is that it is executed in separate subshell, thats why you cannot use one variable before/after the loop and inside it:

#!/bin/bash
OUR_VARIABLE="Start of the script"
echo "Now OUR_VARIABLE is '$OUR_VARIABLE'"
echo '---'
cat /dev/urandom |head -n 3 |while read LINE; do
 echo "Setting OUR_VARIABLE to 'Inside loop'"
 OUR_VARIABLE='Inside loop'
done
echo '---'
echo "OUR_VARIABLE is still '$OUR_VARIABLE'"

The output will be:

Now OUR_VARIABLE is 'Start of the script'
---
Setting OUR_VARIABLE to 'Inside loop'
Setting OUR_VARIABLE to 'Inside loop'
Setting OUR_VARIABLE to 'Inside loop'
---
OUR_VARIABLE is still 'Start of the script'

So how we can handle with it? Really simple:

#!/bin/bash
echo "Lets try other while loop"
echo '---'
OUR_VARIABLE="Start of the script"
echo "Now OUR_VARIABLE is '$OUR_VARIABLE'"
while read LINE; do
 echo "Setting OUR_VARIABLE to 'Inside loop'"
 OUR_VARIABLE='Inside loop'
done < <(cat /dev/urandom |head -n 3) #our secret ingridient
echo '---'
echo "Now OUR_VARIABLE is '$OUR_VARIABLE'"

Now the output will be:

Lets try other while loop
---
Now OUR_VARIABLE is 'Start of the script'
Setting OUR_VARIABLE to 'Inside loop'
Setting OUR_VARIABLE to 'Inside loop'
Setting OUR_VARIABLE to 'Inside loop'
---
Now OUR_VARIABLE is 'Inside loop'

3. Working behind a corporate proxy is pain, everyone knows it. Especcialy, if you have AD-domain username. Most well-written linux apps use system variables http_proxy and https_proxy to get proxy settings. You can set these variables in command line:

export http_proxy="http://domain_name\\user_name:password@proxy_host:8080"

But in that case your password will be saved in bash history and showed in console. Also you can set the variables in your .bash_profile, but saving your domain corporate password in text file is not a good idea.
This little script will help you to stay secured and get a little less pain while setting the proxy:

#!/bin/bash
echo "Enter domain username:"
read NAME
echo "Enter domain password:"
read -s PASSWORD
export http_proxy="http://domain_name\\$NAME:$PASSWORD@proxy_host:8080"
export https_proxy="http://domain_name\\$NAME:$PASSWORD@proxy_host:8080"
echo "Proxy set successfully."
exec $SHELL -i

Do you have bash tricks & hacks that you use every day? Post it in comments!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s