eECHO BLOG

A journey of a thousand miles starts with a single step.

Bash

Bash is the gnu undertaking ‘s require spokesperson for unix. Bash is a posix-compliant beat with full bourne beat syntax and some c require establish in. It was pen by brian fox. The name stand up for bourne-again shell. It is licence as free software package below the gnu gpl.

Bash is a posix casing with a keep down of extensions. It is the casing for the gnu manoeuver system of rules from the gnu project. It can be run on most unix-like manoeuver systems. It is the default casing on most system of rules reinforced on top of the linux inwardness as well as on mac os x and darwin. It has also been ported to microsoft windows apply subsystem for unix-based practical application ( sua ), or the posix emulation render by cygwin and msys. It has been ported to dos by the djgpp see and to novell netware.

The bash control sentence structure is a superset of the bourne eggshell control syntax. The vast majority of bourne eggshell playscript can be run by bash without change, with the elision of bourne eggshell playscript bumble into bang sentence structure demeanour rede other than in bash or seek to run a arrangement control equalise a new bash builtin, etc. Bash control sentence structure include thought draw from the korn eggshell ( ksh ) and the c eggshell ( csh ) such as control line redaction, control account, the directory batch, the $ random and $ ppid variable, and posix control permutation sentence structure $ ( … ). When used as an interactional control eggshell and pressing the tab key, bash mechanically uses control line completion to check partly typewrite political platform call, computer filename and variable star call.

Bash ‘s sentence structure has many extension service which the bourn trounce lacks. Bash can perform integer figuring without spawn international sue, unlike the bourn shell. Bash uses the ( ( … ) ) instruction and the $ ( ( … ) ) variable quantity sentence structure for this purpose. Bash sentence structure simplifies i/o redirection in ways that are not possible in the traditional bourn shell. For object lesson, bash can redirect measure outturn ( stdout ) and measure error ( stderr ) at the same time habituate the & > operator. This is childlike to type than the bourn trounce equivalent ‘command

> file 2 > &1'

. When habituate the ‘function ‘ keyword, bash single-valued function resolve are not compatible with bourne/korn/posix script ( the korn trounce has the same trouble when habituate ‘function ‘ ), but bash live with the same single-valued function resolve sentence structure as the bourn and korn trounce, and is posix conformant. Due to these and early difference, bash trounce script are seldom runnable below the bourn or korn trounce representative unless by design publish with that compatibility in mind, which is turn less common as linux turn more widespread. But in posix mode, bash conformance with posix is most everlasting.

Bash abide here papers just as the bourn blast, has. All the same, since translation 2. 05b bash can redirect measure stimulant ( stdin ) from a ” here strand ” exploitation the

< < <

wheeler dealer.

Bash 3. 0 defend in-process even locution equal victimization a phrase structure evocative of perl.

Bash 4. 0 stick out associatory align allow for wangle stick out for multi-dimensional align, in a standardised way to awk :

Bash Tutorial (examples)

Which shell am i running?

grep yourloginname /etc/passwd

/etc/profile

Configuration file for bash any data like variables, path, start options, hotsname, Histsize etc..

/etc/bashrc

Aliases and functions any logic
It is to make changes globaly.

/etc/skel/

Contains hidden files .bash_logout .bash_profile .bashrc this setting is for all user on the system. It is to make changes for eth new user.

To add new variable or set exisistend one use /etc/profile file and export it.
Example:

export EECHOSITE=www.eecho.info

convention for name of variable is upercase.

To see this variable in interactive shell use ( set /printenv / env)

set | grep eechosite

Test

$? output of last executed command 0 is succes full any other code means error is acureded

Integers

test 1 -eq 1

echo $? return 0 it is true in bash.

test 100 lt 200

echo $? return 0

test 100 -ne 200; echo $?

will return 0

String

test hello_world = hello world; ehco $?
0

test hello_world != hello world; ehco $?
1

Files

Create two files for comparition

touch testfile.sh testfile2.sh

Compare files

test testfile1 -nt testfile2; echo $?
1

test testfile1 -ot testfile2; echo $?
0

Test Char device

test -c /dev/cua0

Test of file exist

test -f testfile3

folder files and links

test -e testfile3

Test andere syntax

[ -f testile ]

Make file executable with chmod

chmod +x test.sh

Referring to arguments

for x
do
    echo "Converting $x"
    tr '015' '\012' < "$x" > "tmp.$x"
    mv "tmp.$x" "$x"
done

test.sh file1 file2 file3

Text editing manipulation

cat – concatenate files and print on the standard output
wc – print newline, word, and byte counts for each file
grep – searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN.
vim – IMproved, a programmers text editor
cut – Print selected parts of lines from each FILE to standard output.

Examples:

Save line count of a file in a variabel

linecount=`wc -l synonyms.txt`
echo $linecount
5000 synonyms.txt

$ wc -l synonyms.txt | cut -d ' ' -f 1
76563
$ wc -l synonyms.txt | cut -d ' ' -f 2
synonyms.txt

Number of files in current directory

ls -A | wc -l

Last access time

ls –time=atime -ltr synonyms.txt
-rw-r–r– 1 sergej sergej 4796720 2009-08-03 21:32 synonyms.txt

To see how long run script or command

time sleep 3

watch – execute a program periodically, showing output fullscreen

date – print or set the system date and time

expr – evaluate expressions

expr 51 + 50
101
expr 51 \* 50
2550

i=`expr $i + 1`

Comments are closed.