Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of commonly used scripts and functions in linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of commonly used scripts and functions in linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

# find whether the specified directory exists in the current directory, and if not, create it

The code is as follows:

Function mkdir_1

{

If test!-d $1

Then

Mkdir $1

Fi

}

# replace the "prefix=. *" string in the file with "prefix=/home/gnome-unicore-install2/usr/"

# can be used as a reference for sed usage

The code is as follows:

Function modify_prefix

{

Chmod + w $1

Cp $1 $1.bak

Sed 's/prefix =. * / prefix=/home/gnome-unicore-install2/usr/g' $1.bak > $1

Rm $1.bak

}

# replace the string "^ LDFLAGS =. *" in the specified file with "LDFLAGS =-rdynamic-lgdk_pixbuf-lgtk-lgdk-lgmodule-lglib-ldl-lXext-lX11-lm"

The code is as follows:

# change_gnome-config FILENAME

Function change_gnome-config

{

Cp $1 $1.bak

Sed's / ^ LDFLAGS =. * / LDFLAGS =-rdynamic-lgdk_pixbuf-lgtk-lgdk-lgmodule-lglib-ldl-lXext-lX11-lm / g'$1.bak > $1

Rm $1.bak

}

# Delete the line containing the specified character in the specified file

# format: delete_line filename "word_contain"

The code is as follows:

Function delete_line

{

Chmod + w $1

Cp $1 $1.bak

Cat $1.bak | grep-v-e "$2" > $1

}

# purpose: delete files containing line1 or (and? ) the line of line2

# format: delete_line filename line1 line2

The code is as follows:

Function delete_line_no

{

Chmod + w $1

Cp $1 $1.bak

Sed $2 million / 3 hours'$1.bak > $1

Rm $1.bak

}

# purpose: insert the string CONTENT in the line specified by LINE_NO

# can be used as a reference for sed usage

# format: add_line FILENAME LINE_NO CONTENT

The code is as follows:

Function add_line

{

Chmod + w $1

Cp $1 $1.bak

Sed-e $2'i' "$3"'$1.bak > $1

Rm $1.bak

}

# purpose: check the program containing "PC24" code and print it out

# format: check_PC24 / / after installation

The code is as follows:

Function check_PC24

{

Echo "now comes the PC24 checking..."

. $COMMAND_UNICORE/shell/shell_PC24 > & / dev/null

If test-s $COMMAND_UNICORE/PC24_result

Then:

Echo "The following file contains PC24 problems: $COMMAND_UNICORE/PC24_result"

Else

Echo "No PC24 problem found"

Fi

}

# print title

The code is as follows:

Displayheader () {

Echo "* *"

Echo "* IeeeCC754 testing tool *"

Echo "* *"

Echo ""

}

# how to print a menu

The code is as follows:

Displayplatformmenu () {

# clear the screen

Clear

Displayheader

Echo "a) SunSparc"

Echo "b) IntelPentium"

Echo "c) AMD"

Echo "d) Unicore32"

Echo "e) Unicore32 (with FP2001)"

Echo ""

Echo-n "select a Platform >"

}

# receive a menu input

The code is as follows:

Displayplatformmenu

Read answer

Case ${answer} in

A) TARGET= "BasicOp"

B) TARGET= "Conversion"

*) badchoice

Esac

# find whether the file_name file exists in the current directory

# can be used as a reference for if usage

The code is as follows:

Detectfile_name () {

If [!-f file_name]

Then

Echo "Error: file_name does not exist. Please check"

Exit 1

Else

Echo "OK,the directy is exist"

Fi

}

# converts all file names and directory names to lowercase for one or more directory entries specified by the parameter and under the multi-level subdirectories below them.

The code is as follows:

Cvitem ()

{

Echo "mv $1 `dirname $1` / `basename $1 | tr

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'' abcdefghijklmnopqrstuvwxyz' `"

}

[$# = 0] & & {echo "Usage: lcdir item1 item2..."; exit;}

For item in $* # can be used as a reference for the usage of for

Do

["`item`"! = "`basename $item`"] & & {

[- d $item] & &

{

For subitem in `ls $item`

Do

Cvlc $item/$subitem

Done

}

Cvitem $item

}

Done

# an example of login

The code is as follows:

If ($? path) then

Set path= ($HOME/bin $path)

Else

Set path= ($HOME/bin / usr/bin.)

Endif

If (! ${? DT}); then

Stty dec new

Tset-I-Q

Endif

Set mail=/usr/spool/mail/$USER

# examples of the use of if

# check the existence of a command or program before executing it

The code is as follows:

If [- x / sbin/quotaon]; then

Echo "Turning on Quota for root filesystem"

/ sbin/quotaon /

Fi

# get Hostname

#! / bin/sh

The code is as follows:

If [- f / etc/HOSTNAME]; then

HOSTNAME= `cat / etc/ HOSTNAME`

Else

HOSTNAME=localhost

Fi

# if a profile allows several locations, such as crontab, you can use if then elif fi to find it

The code is as follows:

If [- f / etc/crontab]; then # [- f / etc/crontab] is equivalent to test-f / etc/crontab

CRONTAB= "/ etc/crontab"

Elif [- f / var/spool/cron/crontabs/root]; then

CRONTAB= "/ var/spool/cron/crontabs/root"

Elif [- f / var/cron/tabs/root]; then

CRONTAB= "/ var/cron/tabs/root"

Fi

Export CRONTAB

# use uname to judge the current system and do different things for each system.

The code is as follows:

SYSTEM= `uname-s`

If [$SYSTEM = "Linux"]; then

Echo "Linux"

Elif [$SYSTEM = "FreeBSD"]; then

Echo "FreeBSD"

Elif [$SYSTEM = "Solaris"]; then

Echo "Solaris"

Else

Echo "What?"

Fi

# examples of using while

# unconditional loop

The code is as follows:

While:; do

Echo "do something forever here"

Sleep 5

Done

This is the end of this article on "sample analysis of scripts and functions commonly used in linux". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report