Username and password with wget/curl

 

To download a url with basic authentication we need to pass username & password.

To acheive this functionality with wget or curl observer following examples:

With curl:

$ curl -u 'username:password' 'http://www.example.com'

With wget:

$ wget --user='username' --password='password' 'http://www.example.com'

Wget with cookie:

Login to server & save cookie to a file:
$ wget --save-cookies cookiesFile.txt --post-data 'user=username&password=password' http://www.example.com/login.html

Download a page by using saved cookie file:
$ wget --load-cookies cookiesFile.txt -p http://www.example.com/somecontent.html

awk print Nth line after matching a pattern

 

By using awk we can print only Nth line after matching a pattern.

Lets say I have a file test.txt with following content and I want to extract 5 line after matching pattern.

$ cat test.txt

1
2
3
4
5
6
7
8
9

Use following awk code to extract Nth line:

Syntax:

$ awk 'c&&!--c;/pattern/{c=N}' file

where “pattern” is your input pattern and N is a line number to extract after matching pattern.

$ awk 'c&&!--c;/1/{c=5}' test.txt

Output:

6

In above example I am trying to extract 5th line after matching pattern, here my input pattern is 1 and 5th line after matching pattern is 6. So our output is 6.

bash loop though multi line variable

 

I have a multi line variable in my bash script and wanted to iterate through each line of a variable.

To do this task I am using while loop.

Following is my variable:

var="a
b
c
d
e"

As I mentioned to loop through my multi line variable(var) I am using while as shown below:

while read -r line
do
  echo "line: $line"
done <<< "$var"

Just carefully observer last line in above code, here I am using three less than symbols(<<<) and variable in double quotes.

Output:
line: a
line: b
line: c
line: d
line: e

Above example only works with bash.

Bash get nth argument

 

Bash arguments are stored in a array.

Getting nth argument is same as getting nth element from an array.

To get nth argument or nth element from an array use following code in your bash script:

n=4
echo ${!n}

where n=4 is to get 4th argument.

I have added above code in bash script and named it as temp.sh

Following is code in my script temp.sh

#!/bin/bash
n=4
echo ${!n}

By using following command execute temp.sh script:

$ bash temp.sh a b c d e

Output:

d

Since 4th argument for our script is d we got output as d.

Another way to get nth argument or nth element from an array is by suing array slicing.

Instead of using echo ${!n} use echo ${@:$n:1} in temp.sh script.

With array slicing my script temp.sh looks like following:

#!/bin/bash
n=4
echo ${@:$n:1}

Again execute script temp.sh:

$ bash temp.sh a b c d e

Output:

d

** Both ways explained in this article only works with bash **

String comparison with Bash

 

In this article I will explain bash specific string comparison.

Here my input string is “abcdef” and defined it as following:

S="abcdef"

To check if a string startswith:

if [[ "$S" == abc* ]]
then
 echo true
else
 echo false
fi

Output:

true

To check if a string endswith:

if [[ "$S" == *def ]]
then
 echo true
else
 echo false
fi

Output:

true

To check if a string contains:

if [[ "$S" == *cd* ]]
then
 echo true
else
 echo false
fi

Output:

true

After writing any of the above comparison in a script you need to run it with bash only.

Lets say if we are written any of the above code in test.sh script, run it with following command:

$ bash test.sh