Common script and function sharing in Linux _linux shell

Source: Internet
Author: User
Tags chmod

#查找当前目录中是否存在指定目录, if it does not exist, it is created

Copy Code code as follows:

function mkdir_1
{
if test! -D $
Then
mkdir $
Fi
}

#指定文件中的 "prefix =. *" string replaced with "prefix=/home/gnome-unicore-install2/usr/"
#可以用来作为sed用法的参考
Copy Code code as follows:

function Modify_prefix
{
chmod +w $
CP $ $1.bak
Sed ' s/prefix =. */prefix=/home/gnome-unicore-install2/usr/g ' $1.bak > $
RM $1.bak
}

#指定文件中的 "^ldflags =.*" string replaced with "Ldflags =-RDYNAMIC-LGDK_PIXBUF-LGTK-LGDK-LGMODULE-LGLIB-LDL-LXEXT-LX11-LM"
Copy Code code as follows:

#change_gnome-config FILENAME
function Change_gnome-config
{
CP $ $1.bak
Sed ' s/^ldflags =.*/ldflags =-rdynamic-lgdk_pixbuf-lgtk-lgdk-lgmodule-lglib-ldl-lxext-lx11-lm/g ' $1.bak> $
RM $1.bak
}

#删除指定文件的含有指定字符的行
#格式: delete_line filename "Word_contain"
Copy Code code as follows:

function Delete_line
{
chmod +w $
CP $ $1.bak
Cat $1.bak | Grep-v-E "$" >$1
}

#用途: Delete file contains line1 or (and). ) The Line2 Line
#格式: delete_line filename line1 line2
Copy Code code as follows:

function Delete_line_no
{
chmod +w $
CP $ $1.bak
Sed $2,$3 ' d ' $1.bak>$1
RM $1.bak

}


#用途: Inserts a string content in the line specified by Line_no
#可以用来作为sed用法的参考
#格式: Add_line FILENAME line_no CONTENT
Copy Code code as follows:

function Add_line
{
chmod +w $
CP $ $1.bak
SED-E $ ' i ' "$" ' $1.bak > $
RM $1.bak
}

#用途: Check the program that contains "PC24" code and print it out
#格式: Check_pc24//after Installation
Copy Code code 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
}

#打印标题
Copy Code code as follows:

Displayheader () {
echo "*****************************************"
echo "* IeeeCC754 testing Tool *"
echo "*****************************************"
echo ""
}

#打印一个菜单的做法
Copy Code code 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 >"
}

#接收一个菜单输入
Copy Code code as follows:

Displayplatformmenu
Read answer
Case ${answer} in
A) target= "Basicop";;
b) target= "conversion";;
*) Badchoice;;
Esac

#查找当前目录下是否存在file_name文件
#可以用来作为if用法的参考
Copy Code code 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
}

#将参数指定的一个或多个目录项以及其下的多级子目录下的所有文件名和目录名转换为小写.
Copy Code code as follows:

Cvitem ()
{
echo "MV $ dirname $/basename $ | Tr
' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' abcdefghijklmnopqrstuvwxyz ' "
}
[$# = 0] && {echo usage:lcdir item1 item2 ... "; Exit }
For item in $* #可以用来作为for用法的参考
Todo
["' DirName $item '"!= "' basename $item '] && {
[D-$item] &&
{
For subitem in ' LS $item '
Todo
CVLC $item/$subitem
Done
}
Cvitem $item
}
Done

#一个login的例子
Copy Code code 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

#关于if使用的几个例子
#执行一个命令或程序之前, check that the command exists before executing
Copy Code code as follows:

if [-x/sbin/quotaon]; Then
echo "Turning on Quota for root filesystem"
/sbin/quotaon/
Fi

#得到Hostname
#!/bin/sh
Copy Code code as follows:

if [-f/etc/hostname]; Then
Hostname= ' Cat/etc/hostname '
Else
Hostname=localhost
Fi

#如果某个设定档允许有好几个位置的话, such as crontab, can use if then elif fi to find
Copy Code code as follows:

if [-f/etc/crontab]; Then #[-f/etc/crontab] 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

#利用uname来判断目前系统 and do different things for each system.
Copy Code code 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

#关于while使用的例子
#无条件循环
Copy Code code as follows:

While:; Todo
echo "Do Something Forever"
Sleep 5
Done

Related Article

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.