source : http://linuxcommand.org/lc3_writing_shell_scripts.php

 

Writing shell scripts

 

a shell script is file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line. 

 

a shell script is a file that contains ascii text.

to create a shell script, you use a text editor. 

 

Use vi to use text editor, make sure to designate a folder name after typing the vi command 

 

#!/bin/bash

# My first script

 

echo "Hello World!"

 

And then… set permission as it follows, 

Chmod 755 hello_world

 

You could ./hello_world, but setting a path where all the files are stored in would be much more easily accessible. For example, all the commands are stored at $PATH. You could add your own path directory by the following

 

Export PATH=$PATH:directory

 

Now just typing hello_world would run your script.

 

 

A. Editing the scripts you already have

 

there are two kinds of shell session.

a login shell session is one that prompts for username and password.

a non-login shell session typically occurs for gui. 

 

~/.bashrc = a user's personal start up file. could make your own alias scripts here and you can use the commands regularly.

 

access the .bashrc file by: 

vi .bashrc

 

and add the following 

 

today() {

  echo -n "Today's date is: "

  date + "%A, %B %-d, %Y"

}

 

and restart your terminal to take effect and type today will output :

B. Here Scripts

 

Create a HTML using script

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
 
# sysinfo_page - A script to produce an HTML file
 
cat <<- _EOF_
    <html>
    <head>
        <title>
        The title of your page
        </title>
    </head>
 
    <body>
        Your page content goes here.
    </body>
    </html>
_EOF_
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

and then run:

sysinfo_page > sysinfo_page.html

 

C. Variables

 

How to create variables : 

variables can be declared on top of shell script as global variables and be used be adding $variablename

 

variablename="your text here"

there should be no spaces!

 

variable that comes with the system are called environment variables. environment variables can be checked by typing printenv.

by convention, environment variables are uppercase

 

D. Command Substitution and Constants

 

$() tells the shell to substitute the results of the enclosed command. 

There is an alternate syntax for $(), which is backticks ``. however ``backticks are older form and for readability, $() is much more prefered.

 

right_now=$(date +"%x %r %Z")

this is also possible

 

D.Shell Functions

 

For maintains and readability, it is often useful to break a single, large task into a series of smaller tasks.

 

Process of identifying the top-level steps and developing increasingly detailed views of those steps is called top-down design. 

 

When you are developing a program, it is is often a good practice to add a small amount of code, run the script, add some more code, run the script, and so on. This way, if you introduce a mistake into your code, it will be easier to find and correct.

 

stubbing = havent developed the function but instead of leaving it blank, could add a basic command like echo to run the script.

 

E. Flow Control - Part 1

 

The if statement has the following syntax:

if commands; then

commands

[elif commands; then

commands...]

[else

commands]

fi

 

Expression

Description

-d file

True if file is a directory.

-e file

True if file exists.

-f file

True if file exists and is a regular file.

-L file

True if file is a symbolic link.

-r file

True if file is a file readable by you.

-w file

True if file is a file writable by you.

-x file

True if file is a file executable by you.

file1 -nt file2

True if file1 is newer than (according to modification time) file2

file1 -ot file2

True if file1 is older than file2

-z string

True if string is empty.

-n string

True if string is not empty.

string1 = string2

True if string1 equals string2.

string1 != string2

True if string1 does not equal string2.

 

**Commands issue a value to the system when they terminate, called an exit status. 

integer range from 0 to 255, 

An 8-bit unsigned integer has a range of 0 to 255, while an 8-bit signed integer has a range of -128 to 127 - both representing 256 distinct numbers.

 

as seen here, success command always outputs 0. however unsuccesful command results 2.

output nothing if it is false. just like all other programming languages, the basic set of rules are applied. 

 

id -u displays current users id. if your are a superuser the number will be 0.

 

>&2 is another I/O direction like cat << _EOF_. >&2 redirects the error message to standar error. this is used to separate error message and normal output. 

 

F. Stay out of trouble

 

when assigning value to a variable, empty value could be placed.

 

number=

 

but when including it to the if statement, you must include "" quotation marks around $number or empty value will be read as an equal sign, causing the script the not work properly.

 

add "-x" beginning of your script file to see your script run on terminal. 

 

G. Keyboard Input and Arithmetic

 

read command takes input from keyboard.

 

#!/bin/bash

echo -n "Enter some text > "

read text

echo "You entered: $text"

 

 

H. Flow Control - Part 2

 

The case command has the following form:

case word in

    patterns ) commands ;;

esac

 

 

looping

 

#!/bin/bash

number=0

while [ "$number" -lt 10 ]; do

     echo "Number = $number"

     number=$((number + 1))

done

 

I. Positional Parameters

 

 

'ETC' 카테고리의 다른 글

Basic CSS  (0) 2020.02.01
FileZilla connect using .ppk(01/31/20 cudo notes2)  (0) 2020.01.31
Linux commands 1  (0) 2020.01.30
Setting aws ec2 and rds, install java jdk8 on ec2(1/30/20 cudo notes1)  (0) 2020.01.30
UnsupportedClassVersionError  (0) 2020.01.29

+ Recent posts