Saturday, November 1, 2014

Shell Script

What are shell scripts?
Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard.

To successfully write a shell script, you have to do three things:
  1. Write a script
  2. Give the shell permission to execute it
  3. Put it somewhere the shell can find it

1. Write the script

The first line of the script is important. This is a special clue given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash.

#!/bin/bash
# My first script

echo "Hello World!"

2. Setting permissions:

chmod 755 my_script

The "755" will give you read, write, and execute permission. Everybody else will get only read and execute permission.

3. Put it somewhere the shell can find it

instead of calling the script via its complete path, you may do the following:
A. put all your scripts in a directory called "bin", under your home directory
B. add this directory to your PATH variable, by typing:
export PATH=$PATH:/home/bin

The PATH variable contains some paths, when a command is typed, the shell search these paths for the program with the name you typed.

Type of commands:

To know the type of a command you can use:

Type command_name

We have 4 types of commands:
1. Commands implemented in the shell. (Pwd - ls - ...)
2. Executables
3. Aliases
You can create an alias by typing:
Alias l = "ls -l"
Then you can use this alias by typing:
l
This will make the same output like "ls -l"
4. Shell functions

function today {
   echo "Today's date is:"
   date +"%A, %B %-d, %Y"
}

The above function is a shell function that can be executed by typing:
today

Aliases and shell functions can be defined in the shell by typing the definition as above. They will be undefined when you close the shell.
They also can be defined in the file ".bash_profile" which is executed each time you log in. And the aliases and functions defined in it will not be undefined. This file exists in your home directory.
The better practice than defining your aliases and functions in ".bash_profile" is to define them in ".bashrc", which is in your home directory and is included inside ".bash_profile".

Variables vs Environment Variables:

Variables are defined as follows:
var_name=value
And used as follows:
$var_name
A variable will be undefined when the shell is closed.

Environment variable is a variable that is defined by some shell scripts that run when the user log on. They are not undefined when you close the shell.
You can list the env vars by typing:
printenv

When a variable is not expected to be changed (expected to be a constant) the convention is to write its name in uppercase. Environment variables names are usually written in uppercase as their values are rarely changed.

To know more about a command:

1. man command
2. command --help
3. Get the path of the script and read it
which command (the path will appear)
strings comd_path (returns the readable parts of the file content)
And you can read the file itself

Substitution:

1. $var_nam
This will tell the shell to substitute the variable name with the variable value
2. $(command)
This will tell the shell to substitute the command with its result (output)

Quoting:

Single quotes limit the substitution:
echo "My host name is $HOSTNAME."
My host name is linux box.

echo 'My host name is $HOSTNAME.'
My host name is $HOSTNAME.

While double quotes limit the wildcards. Try (echo *) and (echo "*").

A backslash is a special quoting character that ignores special characters. It is also called escape character.

sudo vs gksudo:

sudo is a command that allows a permitted user to perform some root processes, by entering the password of the root.
This is to guarantee that no one except the root will make critical processes such as changing permisions, install/uninstall applications.

gksudo is a front end for sudo, that opens gui apps, that needs superuser rights, without running X terminal emulator


How to send email from bash?

sudo apt-get install mutt

Edit and add this to your ~/.muttrc file:


set imap_user = "username@gmail.com"
set imap_pass = "password"

set smtp_url = "smtp://username@smtp.gmail.com:587/"
set smtp_pass = "password"
set from = "username@gmail.com"
set realname = "Your Real Name"

set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"

set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates

set move = no


Start mutt once and send a message, setting accepting certificates to "always"

Then you can send a message via the command line:

echo "Message body" | mutt -s "Subject" phonenumber@carrier.com

No comments:

Post a Comment