eEcho blog

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

Bash

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.