Shell writing example

Source: Internet
Author: User
Write the shell example-general Linux technology-Linux programming and kernel information. The following is a detailed description. Author: junky

I don't like linux for a day or two. I won't talk about it for too many reasons. So I like to try it out for linux. As a cool human-computer interaction interface shell in linux, it is also very nice. Of course, I can only count as a newbie for linux.

Read the script I wrote: Haha. (I have never written a very long script. Basically, I use it as C, but it does not have some functions of C. No comments.
CODE :#! /Bin/sh
# File: gen-cmd.sh
# Generate the cmd line for console

# Need a config file. for example:
# Config file: cmd. conf (not contain '#')
# String: create mac_based_vlan mac_address 00-00-00-00 -::
# Number: 10: 11:
# String :-::
# Number: 10: 11:
# String: vlan default ::

#./Gen-cmd.sh-f cmd. conf-o example .txt
# Then logs .txt is: (not contain '#')
# Create mac_based_vlan mac_address 00-00-00-00-10-10 vlan default
# Create mac_based_vlan mac_address 00-00-00-00-11-11 vlan default

#./Gen-cmd.sh-f cmd. conf-o then .txt-l
# Then logs .txt is: (not contain '#')
# Create mac_based_vlan mac_address 00-00-00-00-10-10 vlan default
# Create mac_based_vlan mac_address 00-00-00-00-10-11 vlan default
# Create mac_based_vlan mac_address 00-00-00-00-11-10 vlan default
# Create mac_based_vlan mac_address 00-00-00-00-11-11 vlan default


CONFIG_FILE = ""
Pai_file = ""
LINE [1] = ""
NUMBER_START [1] = ""
NUMBER_START_BACKUP [1] = ""
NUMBER_END [1] = ""
EOF_SIGN = "_ EOF __"

LAST_COMMAND [1] = ""

ITEM_COUNTER = 0
LINE_COUNTER = 0

LOOP_IN_LOOP = 0

Print_version ()
{
Echo"
**************************************** *********
Generate the cmd line for console
---------------------
Version 1.0:
**************************************** *********"
}

_ Print ()
{
Echo $ *
}

_ Exit ()
{
UKOWN_ERROR =-1
If [-n "$1"]; then
Echo "$1"
If [-n "$2"]; then
Exit $2
Else
Exit $ {UKOWN_ERROR}
Fi
Else
Echo "unkown error"
Exit $ {UKOWN_ERROR}
Fi
}

_ Help ()
{
Echo"
Usage: gen-cmd.sh {-l} {-h} {-f config-file} {-o output-file }"
Exit $1
}

_ Trace ()
{
TRACE_COUNTER = $ [TRACE_COUNTER + 1];
If [-n "$1"]; then
Echo Trace $1! Counter is $ {TRACE_COUNTER}
Else
Echo Trace here! Counter is $ {TRACE_COUNTER}
Fi
}

Load_param ()
{
Echo "Prameter number: $ #, content: $ *"

CONFIG_FILE = "cmd. conf" # default value
Pai_file = "cmd.txt"
LOOP_IN_LOOP = 0

While [$ #-ne 0]; do
Case $1 in
-F) CONFIG_FILE = $2
Shift ;;
-O) pai_file = $2
Shift ;;
-L) LOOP_IN_LOOP = 1;
Shift ;;
-H) _ help ;;
*) _ Print "Illegal Option: $1"
_ Help
Esac
Done

Echo "Config file :$ {CONFIG_FILE}; Output file :$ {pai_file }"
Echo

If [! -F "$ {CONFIG_FILE}"]; then
_ Exit "Can not find the config file :$ {CONFIG_FILE }"
Fi

If [! -R "$ {CONFIG_FILE}"]; then
_ Exit "Permission deny, can not read config file :$ {CONFIG_FILE }"
Fi

>$ {Pai_file}

If [! -W "$ {pai_file}"]; then
_ Exit "Permission deny, can not write output file :$ {pai_file }"
Fi
}

Read_line ()
{
ITEM_COUNTER = $ [ITEM_COUNTER + 1];
LINE_COUNTER = $ [LINE_COUNTER + 1];
Line_temp = $ (sed-n "$ {LINE_COUNTER} p" $ {CONFIG_FILE} | sed's/[\ r \ n] // ')

While [1]; do
If ["$ {line_temp}" = ""]; then
LINE [$ ITEM_COUNTER] =$ {EOF_SIGN} # do not use this string in command
ITEM_COUNTER = $ [ITEM_COUNTER-1];
Return
Fi

Local_temp = $ (echo-n $ {line_temp} | sed's ///')

If ["$ {local_temp: 0: 1 }"! = "#"]; Then
Break
Fi

Echo "Skip @ line $ {LINE_COUNTER }:$ {line_temp }"

LINE_COUNTER = $ [LINE_COUNTER + 1];
Line_temp = $ (sed-n "$ {LINE_COUNTER} p" $ {CONFIG_FILE} | sed's/[\ r \ n] // ')
Done

LINE [$ ITEM_COUNTER] =$ {line_temp}

Local_temp = $ (echo-n "$ {line_temp}" | awk-F' [:] ''{print $1 }')

If ["$ {local_temp}" = "string"]; then
Local_temp = $ (echo-n $ {line_temp} | awk-F '[:] ''{print $2 }')
If ["$ {local_temp}" = ""]; then
_ Exit "Invlid format @ line $ {LINE_COUNTER }:: {local_temp }"
Fi
LINE [$ ITEM_COUNTER] =$ {local_temp}
Echo "String format @ line $ {LINE_COUNTER }:$ {local_temp }"

Read_line

ITEM_COUNTER = $ [ITEM_COUNTER-1];
Return
Elif ["$ {local_temp }"! = "Number"]; then
_ Exit "Invlid format @ line $ {LINE_COUNTER }:: {local_temp }"
Fi

# Number format

NUMBER_START [$ ITEM_COUNTER] = $ (echo-n $ {line_temp} | awk-F' [:] ''{print $2 }')
NUMBER_END [$ ITEM_COUNTER] = $ (echo-n $ {line_temp} | awk-F' [:] ''{print $3 }')

NUMBER_START [$ ITEM_COUNTER] = $ [$ {NUMBER_START [$ ITEM_COUNTER]} + 0] # convert to number
NUMBER_END [$ ITEM_COUNTER] = $ [$ {NUMBER_END [$ ITEM_COUNTER]} + 0] # convert to number
LINE [$ ITEM_COUNTER] = ""

Echo "Number format @ line $ {LINE_COUNTER}: start :$ {NUMBER_START [$ ITEM_COUNTER]}, end :$ {NUMBER_END [$ ITEM_COUNTER]}"

Read_line

ITEM_COUNTER = $ [ITEM_COUNTER-1];
Return
}

Gen_cmd ()
{
ITEM_COUNTER = $ [ITEM_COUNTER + 1];
Local_temp =$ {LINE [$ ITEM_COUNTER]}

If ["$ {local_temp}" = "$ {EOF_SIGN}"]; then
ITEM_COUNTER = $ [ITEM_COUNTER-1];
Echo $ {LAST_COMMAND [$ ITEM_COUNTER] >>$ {detail _file}
Return
Fi

If [-n "$ {local_temp}"]; then # string format
LAST_COMMAND [$ ITEM_COUNTER] = "$ {LAST_COMMAND [$ ITEM_COUNTER-1] }$ {local_temp }"
Gen_cmd
ITEM_COUNTER = $ [ITEM_COUNTER-1];
Return
Fi

# Number format

If [$ {LOOP_IN_LOOP} = 1]; then
NUMBER_START_BACKUP [ITEM_COUNTER] =$ {NUMBER_START [$ ITEM_COUNTER]}

While [$ {NUMBER_START [$ ITEM_COUNTER]}-le $ {NUMBER_END [$ ITEM_COUNTER]}]; do
LAST_COMMAND [$ ITEM_COUNTER] = "$ {LAST_COMMAND [$ ITEM_COUNTER-1] }$ {NUMBER_START [$ ITEM_COUNTER]}"
Gen_cmd
NUMBER_START [$ ITEM_COUNTER] = $ [NUMBER_START [$ ITEM_COUNTER] + 1]
Done

NUMBER_START [ITEM_COUNTER] =$ {NUMBER_START_BACKUP [$ ITEM_COUNTER]}
Elif [$ {NUMBER_START [$ ITEM_COUNTER]}-le $ {NUMBER_END [$ ITEM_COUNTER]}]; then
LAST_COMMAND [$ ITEM_COUNTER] = "$ {LAST_COMMAND [$ ITEM_COUNTER-1] }$ {NUMBER_START [$ ITEM_COUNTER]}"
Gen_cmd
NUMBER_START [$ ITEM_COUNTER] = $ [NUMBER_START [$ ITEM_COUNTER] + 1]
Else # all loops are done!
Echo
Echo
_ Exit "sucess" 0
Fi

ITEM_COUNTER = $ [ITEM_COUNTER-1];
Return
}

# Shell begins! Main entry !!!

Print_version

Load_param $ *

Read_line

Echo
Echo-n "Generating command... please wait ..."

If [$ {LOOP_IN_LOOP}-eq 0]; then
While [1]; do
Gen_cmd
Done
Else
Gen_cmd
Fi

Echo
Echo

_ Exit "sucess" 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.