shell scripting basics example 01

Shell scripting :-

In Linux it has different types of shell but mostly used is Bourne shell scripting .

A shell is a command written in a file , the shell reads the command as the user would type in the terminal .

why shell scripting :-

shell scripting is standardized by POSIX , It is written once and used across the systems.

shell scripting also known for simple to write and understand plus its is easy for development activities.

shell scripting is portable due to POSIX it can be used in different operating system like UNIX, LINUX, AIX etc.

what is procedure to write?a shell script -

In order to create and execute the shell we need to perform three steps .

* write a script (.sh format)

* make the script executable ( required permission using chmod)

* location of the shell script files where shell executable could find.

Lets see a example of shell script.

#!/bin/bash - The shebang is used to tell the kernel the name of the interpreter that should be used to execute the script that follows.

Note :- Every shell script should include #!/bin/bash?as a first line .

As a good practice lets keep the shell script files under /home/ubuntu/bin the user need to create a new directory named bin under home directory like /home/ubuntu/bin

  • create a file name like demo_script.sh?,using vim or vi editor open the file demo_script.sh.
  • once the file is open press i to insert the text and now enter the text like

#!/bin/bash

echo -e "Welcome to shell scripting "

to save the file press esc button then :wq! it will save the file.

Before executing the demo_script.sh make sure it has required permission .

chmod 775 demo_script.sh

ubuntu@ip-172-31-44-226:~/bin$ sh demo_script.sh

In order to execute the .sh file from /home/ubuntu/bin we need to add /home/ubuntu/bin in environmet PATH

open the file vi .bashrc

add below value check your /bin directory where you have created i have created under home/ubuntu/bin

export PATH=~/bin:"$PATH"

ubuntu@ip-172-31-44-226:~/bin$ source ~/.bashrc

ubuntu@ip-172-31-44-226:~/bin$ bash demo_script.sh

?Variables,input and output in shell scripting?

  • A variable name start with a letter?or under score and may contain number,letter,digits and underscores.
  • As Variables holds string value, it has no limit the number of character it holds.

ubuntu@ip-172-31-44-226:~$ my_var_demo=this_is_first_variable_value

ubuntu@ip-172-31-44-226:~$ echo $my_var_demo

this_is_first_variable_value

ubuntu@ip-172-31-44-226:~$

Variables can have multiple assignments for an example

ubuntu@ip-172-31-44-226:~$ first_name=mehdi middle_name=pasha last_name=khan ## multiple assignments allowed

ubuntu@ip-172-31-44-226:~$ fullname="mehdi pasha khan" ## use quotes for white space in value

ubuntu@ip-172-31-44-226:~$ fullname="$first_name$middle_name$last_name"

ubuntu@ip-172-31-44-226:~$

ubuntu@ip-172-31-44-226:~$ oldname=$fullname ## assign new variable to old variable .

ubuntu@ip-172-31-44-226:~$ echo $oldname # quotes not needed to preserve spaces in value.

mehdipashakhan

ubuntu@ip-172-31-44-226:~$

Input :- in shell scripting input is accept with help of read?command .

  • the read command reads a line of text from the standard input and stores the text in a variable

#!/bin/bash

echo -e "Welcome to shell scripting "

read "Enter your name : " name

echo $name

Output :-?

#!/bin/bash

echo -e "Welcome to shell scripting "

first_name="mehdi" last_name="pasha"

read "Enter your name : " name

echo $name

printf "First name :%sLastname:%s" $first_name $last_name

Shell scripting formatting options?

-There are many formatting options i will mention few here

  • syntax highlighting?

:syntax on

  • highlight the search results

:set hlsearch?

  • tab character space?

:set tabstop=4?

  • turns on auto indent feature

:set autoindent


Finally topic of todays post will be about quoting and literals

vi demo_script.sh

#!/bin/bash

echo $100

echo '$100'

echo "$100"


output will be?

00 in shell scripting $1 has a meaning?we can't user $1?

$100 -- printed as a string?

00 --- referenced as variable?

ubuntu@ip-172-31-44-226:~$sh demo_script.sh 4

400

$100

400?

-- here when we pass argument 4 the script accept it as first argument?






要查看或添加评论,请登录

Mehdi Pasha的更多文章

社区洞察

其他会员也浏览了