Search This Blog

Unix

 Unix

Basics:

$ pwd              ---displays current working directory path

$ uname         ----displays operating system name

Linux

$ uname –r     ----displays kernel version name

3.13.0-24-generic

 

$ hostname    -----dispalys server name

hj-glbtal8591

$ hostname -i    ----displays servers IP address

10.51.226.42

$ hostname     --gives host name

cdtssseabcd.abcorp.com

 

$> uptime   -- #To see the server uptime, boot time, users and load

 06:11:56 up 12 days, 10:06, 110 users,  load average: 71.49, 73.23, 72.12

 

 

Who is Logged In?

$ users

root

$ whoami

root

$ logname      ---displays current username

 

$   ---user working prompt

#    ----admin working prompt

 

 

$ exit    ----logout from current user account

$ su - username   ---to switch from one user to another user account

$man <command_name>     ---gives manual pages of acommand

$clear    ----clears screen

$wc <file_name>  ---to count words, lines, bytes

$cd -        ---to go to last working directory.



Viewing list of files:

$ls    ---lists all files and directories in current directory

$ls -l   ---long list format

$ls -a    ---list invisible files

$ls -r     ----lists files in reverse order

$ls -t       -------lists all files based on time

$ls <directory_name>    ---to list files in a directory

$ls -la < directory_name >   ---to list files in a directory

$ ls *{jpg,JPG}     --lists files ending with jpg and JPG

 

Listing commands in current shell:

$ ls /bin


To use single quotes in a command use back slash (\)

---------------------------------- --------------------------------------

Tab keyuse this for auto completion of commands and directory names and file names

------------------------------------------------------- -----------------------------------------------------------



Commands:

Command details:

1)      $ cat  --help

2)      $ info cat

3)      $ man cat

Cat:

For creating a file.

$ cat > hari       -----creates hari file

$ cat < hari       ----- for opening hari file

$ cat >> hari     -----for appending the hari file

$ cat -n <file_name>  ---‘-n’ gives line number’s

How to open multiple files:

$ cat <file1> <file2> <file3>

$cat -b persistent   ----gives line numbers

     1  persistent

     2  its a level 3 company

     3  established in 1990

$ cat > .<file1>    ----creates hidden file(‘.’ Is used for creating hidden files)

 

$ cat /dev/null > hk_test1            --to empty a file

 


Stat:

$ stat hk_test1    --gives stats for a file



Touch:

It is used for creating empty files, more over it is used for changing file time stamp.

 

$ touch <file1> <file2> <file3>      ---creates multiple files

Creating multiple files:

$ touch memo{1,2,3,4,5}

$ ls m*

memo1 memo2 memo3 memo4 memo5

 


Deleting files and creating Directories:

$ rm <file1>     -----deletes file1

$ rm –i <file1>    -----deletes file1 with confirmation

$ rm *      -----removes all files

 

 

$ mkdir <dir_name>   -----creates dir_name directory

$ rmdir <dirname>     ----deletes directory

$ rm -r <dirname>      -----deletes entire directory structure even if it has files.

 

 

Copy:

$ cp <source_file> <targetfile>     ----source file must me existing and target may be new or existing

 


Taking backup to desired location:

cp  /etc/postgresql/9.3/main/abcd.conf  /root/abcd.conf_back_up

 

Relative path:  from current directory to target directory

Absolute path:  from root directory to target directory

 


MV:

To move a file or directory (or) to rename a file or directory.

$ mv hk1 /root/hari   ---hk1 is renamed to hari and moved to root

$ mv hk1 hk2               ----hk1 is renamed to hk2

$ mv .hk1 hk1              -----hk1 hidden file is unhidden

 

SED:

$sed -n 2p <file_name>    ---gives 2nd line



Replacing strings using sed:

$ sed -i -e 's/PERSISTENT/psl/g' hari1    ---replaces in hari1 file

$ sed -i -e 's/PERSISTENT/psl/g' hari1

 

Tr:

$ tr " " "," < hari1   ---replaces space with coma and opens file

PERSISTENT

ITS,A,LEVEL,3,COMPANY

ESTABLISHED,IN,1990



Vi Editor:

Vi has 2 searches 1) string 2) character

/ and ?  Are used for string search

 

Eg:  ?hari (then enter)    ---searches hari string in the file

 

$vi –R <file_name>     ---opens an existing file in read only mode

$view <file_name>     ----opens an existing file in read only mode

:q!   ---quit vi w/o saving

:w   --save

:wq!  ---save and quit from vi

cc     ---removes contents of the line, leaving you in insert mode

U   ---restore current line

u    ---undo thelast change to the file

:f <filename>   ---copies current_file to filename

:e <filename>   ---opens new file filename

:cd dirname   ----changes to directory 

 

 Running commands in Vi:

Command mode à :! ls

 

For replacing a string in vi:

:s/pattern/replace/

:s/search/replace/g

 

Deleting all lines in a file in vi editor:

:1,$d

Or

:%d

 

Deleting specific lines in a file in vi editor:

:4,$-2d   ---leaves first 3 and last 2 lines and deleting the rest.

 

:4,$d      ---leaving first3, rest of the lines are deleted.

:4,$-1d     ---leaving first 3 and last 1line, remaining are deleted

 

How to go to 16 line in Vi:

Esc à 16G

ESCàG     ---goes to last line

 

How to un highlight Searched words in Vi:

goto vi file àESC à :noh àenter

 

 

part2

Changing file Permissions:

$ chmod +x hari     ---giving execution permission to hari

$chmod -111 hari   ---removing execute permission from user, group, others

 

Change file permissions:

Default file permissions are 644, now we will change to 744

$ ls –l persistent

-rw-r--r-- 1 root root    53 Jan 19 19:50 persistent   ----without execution permission

 

$ chmod 744 persistent

$ ls -l persistent

-rwxr--r-- 1 root root 53 Jan 19 19:50 persistent    ----execute permissions received

 

$?        ----gives exit status of the last command executed

$!   ---process number of the last background command

 

Meta characters:

* and ?  are two meta characters.

We use * to match zero or more characters.

 

 

The command could not be located because '/bin' is not included in the PATH environment variable

$export PATH=/usr/bin:/bin ---works fine with this

 

 

.bashrc

                                                                                       

.bash_history:

Has all the previously executed commands.

cat .bash_history

 

SFTP:

Getting file from another UNIX server through sftp:

root@hj-glbtal8591:~# sftp root@11.11.255.55   (user_id@ip_address)   ---logs into SFTP

root@10.51.225.55's password:

Connected to 10.51.225.55.

 

sftp> cd /etc/postgresql/9.3/main    ---go to the folder where file is located

sftp> ls –l                                                    ---check whether file is there or not

-rw-r-----    1 postgres postgres     4649 Oct 21 15:56 abc.conf   ---file exists

-rw-r-----    1 postgres postgres     1636 Oct 20 20:01 bcd.conf

sftp> get abc.conf                         ---getting the file

Fetching /etc/postgresql/9.3/main/abc.conf to abc.conf

/etc/postgresql/9.3/main/pg_hba.conf                                                                                                  100% 4649     4.5KB/s   00:00

sftp> quit                                           ------quit from sftp

root@hj-glbtal8591:~# ls –l        ---check whether file has come

total 2                                                   ---total no:of files in that particular folder

-rw-r-----  1 root root  4649 Jan 16 18:05 pg_hba.conf

drwxr-xr-x  3 root root  4096 Jan 12 21:18 unnati

root@hj-glbtal8591:~#



How do I put (upload) file into another UNIX server using SFTP?

 root@hj-glbtal8591:~# sftp root@11.55.225.55

root@11.55.225.55's password:

Connected to 11.55.225.55.

 

sftp> put pg_hba.conf

sftp> quit

 

 

Some SFTP commands:

sftp> help (or) sftp> ?    ---shows available commands in SFTP

sftp > !ls                 ---to list files in local directory (your machine)

sftp > ls                  ---to list files in remote directory (remote machine)

sftp> mget *.xls    ---Get multiple files on a local system

sftp> !pwd              ­­­--- Local working directory

sftp> pwd               ----Remote working directory(print the current directory on the ftp server)

sftp> put pg_hba.conf    ---put pg_hba.conf file in remote directory

sftp> mput *.xls    ----to put multiple files on remote directory

sftp > cd path          ----Change remote directory to 'path'

sftp> bye    --- Quit sftp

sftp> exit    ---- Quit sftp

sftp> quit    ---- Quit sftp

 

sftp> mkdir persistent           ---Create remote directory

sftp> !mkdir persistent           --- Create local directory(in your machine)

sftp> rmdir persistent           ----remove remote directory

sftp> version                             -----shows FTP version

sftp> !                                         ---go back to local directory/ quit sftp

sftp> !cat > persistent          ---Execute 'command' in local shell/local machine

sftp> rm path                          ---Delete remote file

 

Uninstalling software:

$ apt-get purge <postgresql-server-dev-9.3>

 

Installing software:

$sudo apt-get install <python3-minimal>

wget command examples:

The quick and effective method to download software, music, video from internet is using wget command.

$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz

 

$sudo http_proxy='http://name:pwd@brproxy.company.co.in:8080' apt-get install git

 

 

 

run .bashrc on your current terminal session to source ~/.export file.

echo "source ~/.exports" >> ~/.bashrc                       ???????

 

Connecting and Working on remote host:

ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.

root@hj-glbtal8591:~# ssh -l root 10.51.226.43   ---connecting to remote host

root@hj-glbtal8592:~# exit    ---goes back to original host

 

Awk command examples:

$ awk '{print $2,$5;}' persistent  ---prints 2 and 5 fields from persistent file

 

 

Part3

Vim command examples:

$vim +10 pg_hba.conf    ---goes to the 10 line of pg_hba.conf   

$Cd -     ---takes back to previous working directory

$echo $SHELL   ---Gives shell name

 

Converting all charters in a file to upper case (dd command):

$dd if=persistent of=persistent_new conv=ucase   --if= input file , of=output file, conv=convert, ucase=uppercase

$dd if=persistent_new of=persistent_new1 conv=lcase   ---creates new file persistent_new1 with lowercase chareacters

 

$file *    ---gives all files names and their details in the current directory

 

 

echo "hi krishna, how are you?"          ----prints same

echo "hi krishna,\n how are you?"      ----- \n is used for next line

echo "hi krishna,\r how are you?"       ----- \r is used for removing, preceding it

echo "hi krishna,\t how are you?"       ------ \t is used for giving four spaces

echo "hi krishna,\b how are you?"     -------\b is for back space

echo "\033[1mhi krishna,how are you?\033[0m"    ----\033[1m   is used for printing in bold

                                                                                                \033[0m at the end is used for discontinuing boldness

echo "\033[7mhi krishna,how are you? \033[0m"    ----\033[7m   is used for printing background in black

 

 

 

Zip unzipping files:

$ gzip hari2           ---to zip a file

$ gunzip hari2      ----to unzip a file

 

$ TeradataStudioExpress64__linux_x86_64.16.00.01.00-1.tar.gz   ----file to be unzipped and untarred

$ gunzip TeradataStudioExpress64__linux_x86_64.16.00.01.00-1.tar.gz   ---to unzip a file

$ tar -xf TeradataStudioExpress64__linux_x86_64.16.00.01.00-1.tar          ----to untar a tar file

$ tar -xzf TeradataStudioExpress64__linux_x86_64.16.00.01.00-1.tar          ----to unzip and untar a tar file

ZIP a folder:

$ tar zcvf nameofcompressedfile.tar.gz /path/to/folder/compressthisfolder

 

--we can’t zip a directory in UNIX, hence archive using tar as above.

 

 

 

Scripting:

Using commands in script:

$vi using_command.sh             ---step1 create a file

#using commands in a script   ----step2 writing script in vi

pwd

ls –la

$sh using_command.sh         ---step3 executing script

/root/Krishna                             -----output

total 136

drwxr-xr-x 2 root root  4096 Jan 20 17:54 .

drwx------ 7 root root  4096 Jan 20 17:54 ..

 

Script to read a name:

$vi read.sh

echo "enter your name"

read your_name               ---‘read ‘ is used for taking input

echo "hi $your_name how are you"

$sh read.sh

enter your name

krishna

hi krishna how are you

 

 

Script to rename a file and open:

$vi rename_file.sh

echo "enter file_name"

read file_name    ---Giving the required name (creates new file file_name)

mv persistent $file_name      ---persistent file renamed to file_name

cat $file_name

$sh rename_file.sh

 

Script for reading two file names and renaming file1 to file2:

$vi rename_file1.sh

echo "enter file name1"

read file_1               -----reads file1 which already exists in server

echo "enter file name2"

read file_2              -----reads file2, which is a new file

mv $file_1 $file_2   ----renames file1 to file2

cat $file_2                   ----opens file2

 

 

Positional parameters:

$ set Krishna from Kamalapuram

$ echo $1             ---gives 1st string

Krishna

$ echo $2             ---gives 2nd string

from

$ echo $3             ---gives 3rd string

Kamalapuram

 

 

Script for set Positional parameter:

$ vi posional_parameters.sh

set Krishna from village1

echo $1

echo $2

echo $3

$ sh posional_parameters.sh

Krishna

from

Kamalapuram

 

Set can be used in a script as well as command prompt.

When set is given different sentence then, previous parameters are over written.

 

$ set hari from village2

$ echo $*                            ---we get all the strings

hari from village2

 

Note: Executable files are in Green color.

 

 

 

 

Assigning positional parameters from a file:

$ cat > test

hi Krishna, how are you?

$ set `cat test`

$ echo $*

hi Krishna, how are you?

 

 

 

While loop Script:

$vi while_loop.sh

#script for while loop

a=0

while [ $a -le 5 ]

do

echo $a

a=`expr $a + 1`

done

$sh while_loop.sh 

0

1

2

3

4

5

For loop Script:

$ vi for_loop.sh

#for loop to read text and print it

for i in 1 2 3 4

do echo $i

done

$ sh for_loop.sh

1

2

3

4

 

 

Script to create a file and enter data into it:

$ vi file_creation.sh

#create a file and populate it

cat > hk

echo "from psl" >> hk

$ sh file_creation.sh

Ctrl+d

 

 

 

Script using Break:

$ vi break.sh

#using break to stop a process

a=0

while [ $a -lt 10 ]

do

echo $a

if [ $a -eq 5 ]

then break

fi

a=`expr $a + 1`

done

$sh break.sh

0

1

2

3

4

5

Script for arithmetic operators:

$ vi arithmetic_operators.sh

#arithmatic operators

a=32 b=15

echo `expr $a + $b`

echo `expr $a - $b`

echo `expr $a \* $b`         ----we have to give \ before *

echo `expr $a / $b`

echo `expr $a % $b`         ------gives remainder

$ sh arithmetic_operators.sh

47

17

480

2

2

Note:  expr works only with integer values.

Order of operation: Bracket, division, multiplication, modular division, addition and subtraction (BODMAS)

 

Working with floating numbers:

$vi floating.sh

a=10.5

b=3.5

c=`echo $a + $b | bc`      ---bc is used for working with floating numbers

d=`echo $a - $b | bc`

echo $c $d

 

Script if user is ‘root’ then script is executed

$ vi unix1.sh

#if user is root then script is executed

a=`whoami`

if [ $a = 'root' ]

  then

  echo "authorised"

else

  echo "not authorised"

  exit

fi

echo "hello"

date

$ sh unix1.sh

authorised

hello

Tue Jan 31 18:19:49 IST 2017

 

If Script in korn shell:

$ vi test7.ksh        --step1: open file in vi

#!/bin/ksh          --step2: code in vi

 

a=10

b=10

c=10

if [ $a == $b ] && [ $b == $c ]

then

   echo "a is equal to b and b =c "

else

   echo "None of the condition met"

fi

$ ./test7.ksh     --step3: execution of korn script

 

#!/bin/ksh

 

a=Pass

b=Pass

c=2

if [ $a == Pass ] && [ $b == Pass ] && [ $c=2 ]

then

   echo "Pass "

else

   echo "Fail"

fi

o/p:

cdts10hdbe01d:abwdusr:/home/abwdusr> ./test7.ksh

Pass

 

Script to check date is valid or not:

x=22/02/22222

if [[ $x == [0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9] ]]

then echo "valid"

else echo "invalid"

fi

cdts10hdbe01d:abwdusr:/home/abwdusr> ./test11.ksh

invalid

 

Script to check Column is numeric or not:

#!/bin/ksh

export a=`awk -F',' '{print $3}' prod_test.csv | awk ' $1 ~ "^[0-9][0-9]*$"' |wc -l`

export b=`awk -F',' '{print $2}' prod_test.csv | awk ' $1 ~ "^[0-9][0-9]*$"' |wc -l`

c=`expr $a + $b`

print $c

 


 

 

 

Child Vs Parent Process:

Any process can be a parent and child process at the same time. The only exception is the init process, which is always marked with PID (process ID) 1. Therefore, init is a parent of all processes running on your Linux system.

$ ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:02 init

Any process created will normally have a parent process from which it was created and will be considered as a child of this parent process. For example:

$ echo $$
27861
$ bash
$ echo $$
28034
$ ps --ppid 27861
  PID TTY          TIME CMD
28034 pts/3    00:00:00 bash

  • Line 1: print a PID for a current shell - 27861
  • Line 2: create a new child process from the process ID 27861
  • Line 3: print a PID for a current shell - 28034
  • Line 4: with use of the ps command print the child process of PID 27861

When creating a new child process an export command simply ensures that any exported variables in the parent process are available in the child process.

 

 

 

To find out default login shell:

$ which bash

/bin/bash

(or)

$ who am i

chris pts/0 2011-11-26 07:19 (:0.0)

$ grep chris /etc/passwd

cnegus:x:13597:13597:ravi Negus:/home/cnegus:/bin/bash

 

File last modified time:

$ date -r <file_name>

 

 

 

 

 

Find where particular command came from:

You can use the type command.

e.g.: $ type cat

cat is a tracked alias for /bin/cat

 

 

To view your history list:

Use the history command

e.g.: $ history 0   -- --after logging in it shows from 1st used command to last used command

 

Sequential commands/using multiple commands:

$ date ; uname ; whoami

Wed Jul 26 10:56:51 EDT 2017

Linux

Sdtdusr

 

 

 

 

 

 

Part4

Mailing:

$ mail -s "hi how r u" hk@in.company.com

Ctrl+d    ---to add mail id in CC

 

Ctrl+c

(or)

$ echo "Mail body" | mail -s "Subject" hk@in.company.com --enter

echo "For testing" | mailx -s "Testing" hk@in.company.com --mail and mailx works as same

 

Script to send mail:

vi test.ksh

#!/bin/ksh

MESSAGE="hi How are u?"

SUBJECT="Greeting"

TOADDR="hk@in.company.com"

FROM="DONOTREPLY"

 

echo $MESSAGE | mail -s "$SUBJECT" $TOADDR  -- -f $FROM

 

./test.ksh

 

Script to send CSV file:

#!/bin/ksh

MESSAGE="hi How are u?"

SUBJECT="Greeting"

TOADDR="hk@in.company.com"

FROM="DONOTREPLY"

 

echo "$MESSGE" | mail -s "$SUBJECT" -a  "/development/abc/data/ABC_PRDCT.csv"  $TOADDR  -- -f $FROM

 

--any file can be send like this

 

 

To find a file whose location is not known:

$ vi $(find /home | grep xyzzy)     ----doesn’t workin Kornshell

 

 

Export Usage:

$ m=hk1/hk2/hk3     ----assign m with the directory path hk1/hk2/hk3

$ export m                 ----exporting m

$ cd $m                      -----going to path hk1/hk2/hk3

 

 

 

 

 

 

 

 

 

Echo:

$ echo $$    ---returns process ID of the current shell

 

$ echo $(date)     ---------prints date

Thu Jul 27 06:45:54 EDT 2017

Note: Variables can contain the output of a command or command sequence. You can accomplish this by preceding the command with a dollar sign and open parenthesis, and following it with a closing parenthesis. For example, MYDATE=$(date)

 

$ echo date     -----here date is treated as string

date

 

Calculation:

$ echo "I am $[2012 - 1957] years old."     ---works in Bash Shell

I am 55 years old.

 

Echo:

$ hari=krishna                                                                                                                                   

$ echo ${hari:-"no data"}                                                                                                                        

krishna                                                                                                                                                

$ hari=                                                                                                                                          

$ echo ${hari:-"no data"}                                                                                                                        

no data       

Find a file using echo:

$ echo *txt

--------------------------------------------------------------- ----------------------

$ echo "This is \" a quote and this is \\ a backslash"

This is " a quote and this is \ a backslash

 

So backslash itself must be escaped to show that it is to be taken literally.

 

 

$ alias ls

alias ls='ls --color=auto' --­-The --color=auto option causes different types of files and directories to be                      displayed  in different colors.

 

 

 

 

 

 

Counting no: of files in directory and assigning it to a variable:

$ num_files=$(ls -la | wc -l )

$ echo $num_files

5

 

When assigning variables, use only the variable name (for example, BALANCE). When you reference variable,

meaning you want the value of the variable, precede it with a dollar sign (as in $CurBalance). The result of the

latter is that you get the value of the variable, not the variable name itself.

$ echo "count   :: ${num_files}"                                                                                                                 

count   :: 5

 

 

$ hk1=${num_files-""}                                                                

$ echo $hk1                                                                          

5                                                                                           

$ hk2=${num_files-"hi"}                                                              

$ echo $hk2

5

 

$ THIS="Example"

$ THIS=${THIS:-"Not Set"}

$ THAT=${THAT:-"Not Set"}

 

$ echo $THIS

Example

$ echo $THAT

Not Set

 

 

Shell Program1:

$ vi file4

read -p "Your name? " name

if [[ $name = $USER ]]; then

    echo "Hello, me."

else

    echo "Hello, $name."

fi

 

$ sh file4

Your name? hk

Hello, hk.

--------------------------------------------------------------- -------------------------------------------------------------



Passing a Parameter in shell Script:

1)      Create a ksh file in the pathà /development/abc/exe/

vi hari_practice1.ksh

 

. $1

echo "Hi Hari !" > ${DATA_FOLDER}/hari_practice1.txt

exit 0

 

2)      Create a parm file in the pathà /development/abc/parms/

Vi hari_practice1.parms

 

export DATA_FOLDER=/development/abc/data

 

Note: To run ksh and parameter file.

/development/abc/exe/test_esp.ksh /development/abc/parms/esp_test.parms

 

3)      Now check the file in the path /development/abc/data

 

--------------------- ------------------------------------------------- ----------------------------------------- ------------------


                                                                                                                                         

Usage of NOHUP:

  -- nohup prevents the command from being aborted if you log out or exit the shell.

$nohup run_script.sh > hk1.out &    --& used for running the command in background and note the process id

 

----------------------- ------------------------------------ --------------------------

back-ground Process:

$ ps l <PID>    --to check back-ground Process

$ps -f     ---gives background running jobs

$kill -9 <process_id>   ---to kill background running process.

$cat hari&     ---add at the end of a command to run the process in the back ground

 

------------------------------- -------------------------------------------------- ---------------------------------------------------


 

 

Command to change the shell:

$ chsh -s /bin/ksh

 

dos2unix:

$ dos2unix /home/tptuusr/.profile    --converts a file from dos to UNIX compatible

 

How to check whether dos2unix is performed on a file? :

$ cat -ve <file_name>  ---you will see $ at the end

 

 

Script to create a file by reading input from user:

$ vi script.sh

#!/bin/sh
echo
"What is your name?"
read USER_NAME
echo
"Hello $USER_NAME"
echo
"I will create you a file called ${USER_NAME}_file"
touch
"${USER_NAME}_file"

 

 

 

 

 

For loop:

#!/bin/sh

for i in hello 1 * 2 goodbye

do

  echo "Looping ... i is set to $i"

done

 

Output:

Looping ... i is set to hello

Looping ... i is set to 1

Looping ... i is set to (name of first file in current directory)

    ... etc ...

Looping ... i is set to (name of last file in current directory)

Looping ... i is set to 2

Looping ... i is set to goodbye

 

 

While Loop:

#!/bin/sh

 

while [ "$INPUT_STRING" != "bye" ]

do

  echo "Please type something in (bye to quit)"

  read INPUT_STRING

  echo "You typed: $INPUT_STRING"

done

 

What happens here, is that the echo and read statements will run indefinitely until you type "bye" when prompted

 

 

Script to list all files in a directory:

#!/bin/sh

for i in *

do

echo "$i"

done

 

 

While loop:

#!/bin/sh

 

echo "Please talk to me ..."

while :                              ###colon always evaluates to true

do

  read INPUT_STRING

  case $INPUT_STRING in

        hello)

               echo "Hello yourself!"

               ;;

        bye)

vi             echo "See you again!"

               break

               ;;

        *)

               echo "Sorry, I don't understand"

               ;;

  esac

done

echo

echo "That's all folks!"

 

 

 

Usage of Typeset:

$ typeset -xu h=ram

 $ echo $h

 RAM

    

Curl:

--curl is used for downloading a file from web

 

---------------------------------------------- -------------------

--------------------------------------- -----------------------------------------------

$ rm -rf           --rm removes a file in directory, r –removes all files in a directory recursively  -- forcefully

Ping:

$ ping 8.8.8.8   ---ping lets you know other machine on the internet is live

                          ---8.8.8.8  is Google’s public DNS SERVER

 

Bc:

$ bc   ----it is a calculator program, we can do calculations here , press CTRL+D after using ‘bc’

--------------------- --------------------------------------------------------------------

Searching Large files:

$ less <file _name>   --- we can search page at a time 

                                        --- press ESC q to exit or :q   

                                       --- /production to search production and press n for next occurrence

                                       Even in vi /<word_name> works

----------------------------------- -------------------------------------------------------------------------------------------------------

Note:

File names can contain any character except /  . If file contains spaces or any special characters then put file name in ‘quotes’ or precede each special character with \

------------------------------------------------------------- --------------------------------------------------

 

View:

$ view <file_name>

ESCàG     ---goes to last line in view

 

 

 

 

 

 

 

 

 

Getting total columns from a file:

export Field_count=`head -1 ${FILE} | sed 's/[^,]//g' | wc -c` 

 

 

 

 

 

Validate a date in unix:

 

To get Echo screen set below ones:

#!/bin/ksh
#set -vx
#shopt -s -o nounset
#umask 113

 

 


numeric check on columns

awk -F',' '{print $2}' test1.csv | awk ' $1 ~ "^[0-9][0-9]*$"' |wc -l ---gives total columns with only numeric data

**************** ***********


 value:

$ a=-10

$ b=${a#-}

$ echo $b

10

 

Give row numbers to a file in unix:

$ awk '{printf "%d~%s\n", NR, $0}' test_dtl

 

 

 

 

 

 

Script to add row numbers in other folder than current:

#!/bin/ksh

pwd    echo "gives current path in script"

 

cd /home/abwdusr/hk1/

 

echo "adding row number starts for all files"

 

awk '{printf "%d~%s\n", NR, $0}' dtl.csv > dtl_rowno.csv

 

awk '{printf "%d~%s\n", NR, $0}' prod.csv > prod_rowno.csv

 

echo "adding row number ends"

 

 

script to remove files in some folder:

#!/bin/ksh

cd /home/abwdusr/hk1    #Have to parameterize

 

rm dtl_rowno.csv prod_rowno.csv

 

 

unzipping thru script:

#!/bin/ksh

cd /home/abcdusr/hk1

gunzip dtl.csv

gunzip prod.csv

echo "unzipping ends"

 

 

unzips if any file is .gz format

 

 

 

 

 

 

 

 

 

 

 

Script to create a directory and create a file in it:

#!/bin/ksh

echo "Setting up Env"

 

cd /home/abcdusr

 

echo "Current Directory `pwd`"

 

if [ ! -d "/home/abcdusr/hk2" ]

then

  mkdir hk2

fi

 

echo "removing all the old files"

 

rm -f /home/abwdusr/hk2/*.*


cd /home/abwdusr/hk2

 

echo "hi" > test1

 

echo "

Script Execution Completed @ `date`"



No comments:

Post a Comment