//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

搭建高性能计算环境(三)、安装intel编译器和mpi,intelmpi

很多计算软件都是要从源代码编译的,并且运行也需要mpi的支持,本节我们来安装intel编译器和openmpi。

Linux Shell函数方法简介

 

1、 Intel编译器的安装

需要的软件包:parallel_studio_xe_2013_update2_intel64.tgz、License文件

1)首先将软件包上传的Linux系统,可以使用上节介绍的ssh工具。

2)进入上传的目录,解压缩

tar xvf parallel_studio_xe_2013_update2_intel64.tgz

3)进去解压出来的目录

cd parallel_studio_xe_2013_update2_intel64

4)开始安装

./install

5)欢迎界面

Step no: 1 of 7 | Welcome


Welcome to the Intel(R) Parallel Studio XE 2013 Update 2 for Linux*
installation

program.


You will complete the steps below during this installation:

Step 1 : Welcome

Step 2 : License

Step 3 : Activation

Step 4 : Intel(R) Software Improvement Program

Step 5 : Options

Step 6 : Installation

Step 7 : Complete


Press “Enter” key to continue or “q” to quit:

6)CentOS操作系统会提示不支持,跳过。

Step no: 1 of 7 | Options > Missing Optional Pre-requisite(s)


There are one or more optional unresolved issues. It is highly
recommended to

resolve them all before you continue the installation. You can fix them
without

exiting from the installation and re-check. Or you can quit from the

installation, fix them and run the installation again.


Missing optional pre-requisites

— Intel(R) Composer XE 2013 Update 2 for Linux*: unsupported OS


  1. Skip missing optional pre-requisites [default]

  2. Show the detailed info about issue(s)

  3. Re-check the pre-requisites

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:

7)接受license,输入accept

8)选择激活方式,此处使用文件激活,选择3

Step no: 3 of 7 | Activation


If you have purchased this product and have the serial number and a
connection

to the internet you can choose to activate the product at this time.
Activation

is a secure and anonymous one-time process that verifies your software
licensing

rights to use the product. Alternatively, you can choose to evaluate the

product or defer activation by choosing the evaluate option. Evaluation
software

will time out in about one month. Also you can use license file, license

manager, or remote activation if the system you are installing on does
not

have internet access activation options.


  1. I want to activate my product using a serial number [default]

  2. I want to evaluate my product or activate later

  3. I want to activate either remotely, or by using a license file, or by
    using a

license manager

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:

9)指定license文件

Step no: 3 of 7 | Activation > Advanced activation


You can use license file, license manager, or the system you are
installing on

does not have internet access activation options.


  1. Use a different computer with internet access [default]

  2. Use a license file

  3. Use a license server

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:
2

Note: Press “Enter” key to back to the previous menu.

Please type the full path to your license file(s):
/root/NCOM_L___NT4V-6MS554NC.lic


Activation completed successfully.


Press “Enter” key to continue:

10)是否加入改进,选2就行了。

Step no: 4 of 7 | Intel(R) Software Improvement Program


Help improve your experience with Intel(R) software

Participate in the design of future Intel software. Select ‘Yes’ to give
us

permission to learn about how you use your Intel software and we will do
the rest.

– No Personal contact information is collected

– There are no surveys or additional follow-up emails by opting in

– You can stop participating at any time

Learn more about Intel(R) Software Improvement Program

With your permission, Intel may automatically receive anonymous
information

about how you use your current and future Intel software.


  1. Yes, I am willing to participate and improve Intel software.
    (Recommended)

  2. No, I don’t want to participate in the Intel(R) Software Improvement
    Program at this time.

b. Back to the previous menu

q. Quit


Please type a selection:

11)此处可以修改安装目录和要安装的组件,默认安装到/opt/intel目录下

Step no: 5 of 7 | Options


You are now ready to begin installation. You can use all default
installation

settings by simply choosing the “Start installation Now” option or you
can

customize these settings by selecting any of the change options given
below

first. You can view a summary of the settings by selecting

“Show pre-install summary”.


  1. Start installation Now

  2. Change install directory [ /opt/intel ]

  3. Change components to install [ All ]

  4. Change advanced options

  5. Show pre-install summary

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:

WARNING: Destination directory already exists.

Would you like to overwrite this directory? ( Yes/No ) [ Yes ] :

12)提示我的虚拟机不支持此功能,跳过即可。

Step no: 5 of 7 | Options > Missing Optional Pre-requisite(s)


There are one or more optional unresolved issues. It is highly
recommended to

resolve them all before you continue the installation. You can fix them
without

exiting from the installation and re-check. Or you can quit from the

installation, fix them and run the installation again.


Missing optional pre-requisites

— Intel(R) VTune(TM) Amplifier XE 2013 Update 4: Power analysis is not
enabled


  1. Skip missing optional pre-requisites [default]

  2. Show the detailed info about issue(s)

  3. Re-check the pre-requisites

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:

12)开始安装

Step no: 6 of 7 | Installation


Each component will be installed individually. If you cancel the
installation,

components that have been completely installed will remain on your
system. This

installation may take several minutes, depending on your system and the
options

you selected.


Installing Amplifier XE Command line interface component… done


Installing Amplifier XE Sampling driver kit component…

WARNING: NMI watchdog timer is enabled.

Suggestion: turn off the nmi_watchdog timer before running sampling.


Installing Amplifier XE Power driver kit component… done


Installing Amplifier XE Graphical user interface component… done


Installing Inspector XE Command line interface component… done


Installing Inspector XE Graphical user interface component… done


Installing Advisor XE Command line interface component… done


Installing Advisor XE Graphical user interface component… done


Installing Intel Fortran Compiler XE 13.1 on Intel(R) 64 component…
done


Installing Intel C++ Compiler XE 13.1 on Intel(R) 64 component… done


Installing Intel Debugger 13.0 on Intel(R) 64 component… done


Installing Intel Math Kernel Library 11.0 Update 2 on Intel(R) 64
component… done


Installing Intel Integrated Performance Primitives 7.1 Update 1 on
Intel(R) 64 component… done


Installing Intel Threading Building Blocks 4.1 Update 2 core files and
examples component… done


Finalizing installation… done

Step no: 7 of 7 | Complete


Thank you for installing and using the

Intel(R) Parallel Studio XE 2013 Update 2 for Linux*

Reminder: Intel(R) VTune(TM) Amplifier XE users must be members of the
“vtune”

permissions group in order to use Event-based Sampling.

To register your product purchase, visit

To get started using Intel(R) VTune(TM) Amplifier XE 2013 Update 4:

– To set your environment variables: source

/opt/intel/vtune_amplifier_xe_2013/amplxe-vars.sh

– To start the graphical user interface: amplxe-gui

– To use the command-line interface: amplxe-cl

– For more getting started resources:
/opt/intel/vtune_amplifier_xe_2013/

documentation/en/welcomepage/get_started.html.

To get started using Intel(R) Inspector XE 2013 Update 4:

– To set your environment variables: source

/opt/intel/inspector_xe_2013/inspxe-vars.sh

– To start the graphical user interface: inspxe-gui

– To use the command-line interface: inspxe-cl

– For more getting started resources: /opt/intel/inspector_xe_2013/

documentation/en/welcomepage/get_started.html.

To get started using Intel(R) Advisor XE 2013 Update 2:

– To set your environment variables: source

/opt/intel/advisor_xe_2013/advixe-vars.sh

– To start the graphical user interface: advixe-gui

– To use the command-line interface: advixe-cl

– For more getting started resources: /opt/intel/advisor_xe_2013/

documentation/en/welcomepage/get_started.html.

To get started using Intel(R) Composer XE 2013 Update 2 for Linux*:

– Set the environment variables for a terminal window using one of the

following (replace “intel64” with “ia32” if you are using a 32-bit

platform).

For csh/tcsh:

$ source /opt/intel/bin/compilervars.csh intel64

For bash:

$ source /opt/intel/bin/compilervars.sh intel64

To invoke the installed compilers:

For C++: icpc

For C: icc

For Fortran: ifort

To get help, append the -help option or precede with the man command.

– For more getting started resources:

/opt/intel/composer_xe_2013/Documentation/en_US/get_started_lc.htm.

/opt/intel/composer_xe_2013/Documentation/en_US/get_started_lf.htm.

To view movies and additional training, visit

.


q. Quit [default]


Please type a selection or press “Enter” to accept default choice [q]:
q

[[email protected]
parallel_studio_xe_2013_update2_intel64]#

13)安装完成后设置环境变量

在/etc/profile文件的末尾加入如下命令,重新登录后生效。

source /opt/intel/bin/compilervars.sh intel64

14)确认安装成功

[[email protected]
~]# icc -v

icc version 13.1.0 (gcc version 4.4.6 compatibility)

 

                     开始学习shell 脚本的 应用举例

2、安装openmpi

1)上传安装文件openmpi-1.6.5.tar.bz2,解压缩并进入安装目录

tar xvf openmpi-1.6.5.tar.bz2

cd openmpi-1.6.5

2)使用configure配置,prefix指定安装目录,后面的CC、CXX、FC、F77指定使用intel编译器作为接口

./configure –prefix=/opt/openmpi-165 CC=icc CXX=icpc FC=ifort F77=ifort

3)开始编译,可以使用-j后面跟数字进行多核编译,能减少编译时间。

make –j4

4)编译完成后,使用make install安装到指定的目录

make install

5)设置环境变量

      在/etc/profile文件末尾加入如下行,重新登录后生效。

      export MPI_HOME=/opt/openmpi-165
      export PATH=$MPI_HOME/bin:$PATH
      export LD_LIBRARY_PATH=$MPI_HOME/lib:$LD_LIBRARY_PATH

6)确认安装成功

     
[[email protected]
~]# which mpicc
      /opt/openmpi-165/bin/mpicc
     
[[email protected]
~]# mpicc -v
      icc version 13.1.0 (gcc version 4.4.6 compatibility)

linuxcCentOSDebianFreeBSD 

 

3、intelmpi

1)上传安装程序l_mpi_p_4.1.0.024.tgz和license,可使用ssh工具传输。

2)解压缩安装程序并进入程序目录

tar xvf l_mpi_p_4.1.0.024.tgz

cd l_mpi_p_4.1.0.024

3)开始安装、欢迎界面

./install

Step no: 1 of 6 | Welcome


Welcome to the Intel(R) MPI Library for Linux* OS version 4.1.


You will complete the steps below during this installation:

Step 1 : Welcome

Step 2 : License

Step 3 : Activation

Step 4 : Options

Step 5 : Installation

Step 6 : Complete


Press “Enter” key to continue or “q” to quit:

4)接受license

5)使用license文件激活

Step no: 3 of 6 | Activation


If you have purchased this product and have the serial number and a
connection

to the internet you can choose to activate the product at this time.
Activation

is a secure and anonymous one-time process that verifies your software
licensing

rights to use the product. Alternatively, you can choose to evaluate the

product or defer activation by choosing the evaluate option. Evaluation
software

will time out in about one month. Also you can use license file, license

manager, or the system you are installing on does not have internet
access

activation options.


  1. I want to activate my product using a serial number [default]

  2. I want to evaluate my product or activate later

  3. I want to activate either remotely, or by using a license file, or by
    using a license manager

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:

6)指定激活文件

Step no: 3 of 6 | Activation > Advanced activation


You can use license file, license manager, or the system you are
installing on

does not have internet access activation options.


  1. Use a different computer with internet access [default]

  2. Use a license file

  3. Use a license server

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:
2

Note: Press “Enter” key to back to the previous menu.

Please type the full path to your license file(s):
/root/l_mpi_p_4.1.0.024/intel_mpi.lic


Activation completed successfully.


Press “Enter” key to continue:

7)选择安装目录和安装的组件,此处默认安装

Step no: 4 of 6 | Options


You are now ready to begin installation. You can use all default
installation

settings by simply choosing the “Start installation Now” option or you
can

customize these settings by selecting any of the change options given
below

first. You can view a summary of the settings by selecting

“Show pre-install summary”.


  1. Start installation Now

  2. Change install directory [ /opt/intel/impi/4.1.0.024 ]

  3. Change advanced options [ Current node ]

  4. Show pre-install summary

h. Help

b. Back to the previous menu

q. Quit


Please type a selection or press “Enter” to accept default choice [1]:

Step no: 6 of 6 | Complete


Thank you for installing and for using the Intel(R) MPI Library for
Linux* OS

version 4.1.

Support services start from the time you install or activate your
product. If

you have not already done so, please create your support account now to
take

澳门新葡萄京官网首页,full advantage of your product purchase.

Your support account gives you access to free product updates and
upgrades as

well as interactive technical support at Intel(R) Premier Support.

To create your support account, please visit the Subscription Services
web site

.


q. Quit [default]


Please type a selection or press “Enter” to accept default choice [q]:
q

[[email protected]
l_mpi_p_4.1.0.024]#

8)设置环境变量

在/etc/profile文件末尾加入如下行,重新登录后生效。

source /opt/intel/impi/4.1.0/bin64/mpivars.sh

9)确认安装成功

     
[[email protected]
~]# which mpiicc
      /opt/intel/impi/4.1.0.024/intel64/bin/mpiicc
     
[[email protected]
~]# mpiicc -v
      mpiicc for the Intel(R) MPI Library 4.1 for Linux*
      Copyright(C) 2003-2012, Intel Corporation.  All rights reserved.
      icc version 13.1.0 (gcc version 4.4.6 compatibility)

 

一般我们配置自己熟悉的mpi为默认设置,所以可以再/etc/profile中注释掉不常用的mpi,打开常用的mpi,如有需要可以临时添加或者修改/etc/profile文件后重新登录,使用#注释,如下:

      #setting for intel compiler and mpi
      source /opt/intel/bin/compilervars.sh intel64
      source /opt/intel/impi/4.1.0/bin64/mpivars.sh

      #setting for openmpi
      #export MPI_HOME=/opt/openmpi-165
      #export PATH=$MPI_HOME/bin:$PATH
      #export LD_LIBRARY_PATH=$MPI_HOME/lib:$LD_LIBRARY_PATH

很多计算软件都是要从源代码编译的,并且运行也需要mpi的支持,本节我们来安…

  1. 在shell文件内部定义函数并引用:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

[~/shell/function]# cat factorial.sh 

 

#!/bin/bash

简单的CD管理系统

function factorial

实现功能:  添加CD——a)增加CD的同时给CD增加相应的曲目信息

{

                     删除CD——r)删除指定的CD

factorial=1

                     查找CD——f)查找指定的CD

for (( i=1;i <= $1;i++ ))

                    
更新CD——u)更新指定CD中的曲目信息——重新给该CD添加曲目信息

        do

                     显示CD——l)显示当前指定CD的曲目信息

        factorial=$[ $factorial * $i ]

                     统计CD——c)统计有多少张CD,共有多少曲目

        done

                     退出CD——q)退出CD管理系统

echo $1的阶乘是:$factorial

 

}

代码:

echo ‘程序名’:$0,用于求阶乘

============================= 代码开始 ===============================

factorial $1

#!/bin/bash

[~/shell/function]# ./factorial.sh 10

 

程序名:./factorial.sh,用于求阶乘

 

10的阶乘是:3628800

#### global variable define strat ####

2.返回值

menu_choice=””

函数返回码是指函数最后一条命令的状态码,可以用于函数返回值

current_cd=””

使用return命令手动指定返回值:

title_file=”title.cdb” 
#保存CD的信息,包括CD号码,CD标题,CD类型,CD作者

[~/shell/function]# cat return.sh 

tracks_file=”tracks.cdb”

#!/bin/bash

#保存所有曲目信息,包括CD号码,曲目号码——每张CD由1开始,曲目名称

function fun1 {

temp_file=/tmp/cdb.$$ #临时文件

  read -p “enter a: ” a

trap `rm -rf $temp_file` EXIT #遇到中断时删除临时文件

  echo -n “print 2a: “

#### global variable define end ####

  return $[ $a * 2 ]

 

}

 

fun1

 

echo “return value $?”

#### function define about tools start ####

[~/shell/function]# ./return.sh 

get_return() #返回值函数,需要用户输入需要的返回值,0正常,非0异常

enter a: 100

{

print 2a: return value 200

       echo -e “Press return: c” #显示字符串信息

由于shell状态码最大是255,所以当返回值大于255时会出错

       read x #给变量x读入一个值

[~/shell/function]# ./return.sh 

       return 0

enter a: 200

}

print 2a: return value 144

 

3.函数输出

get_confirm() #确认函数,需要用户输入yes或者no或者y或者n

为了返回大于255的数、浮点数和字符串值,最好用函数输出到变量:

{

[~/shell/function]# cat ./fun_out.sh 

       echo -e “Are you sure? c”

#!/bin/bash

       while true

function fun2 {

       do

  read -p “enter a: ” a

              read x

  echo -n “print 2a: “

              case “$x” in

  echo $[ $a * 2 ]

                     y | yes | Y | Yes | YES) return 0;;

}

                     n | no | N | No | NO) echo

result=`fun2`

                                         echo “Cancelled”

echo “return value $result”

                                         return 1;;

[~/shell/function]# ./fun_out.sh     

                     *) echo -e “Please enter yes or no: c”;;

enter a: 400

              esac

return value print 2a: 800

       done

4.向函数传递参数(使用位置参数):

}

[~/shell/function]# cat ./parameter.sh 

#### function define about tools end ####

#!/bin/bash

 

if [ $# -ne 3 ]

 

then

 

    echo “usage: $0 a b c”

 

    exit

#### set menu chocie function start ####

fi

set_menu_choice() #每次操作选项的显示

fun3() {

{

    echo $[ $1 * $2 * $3 ]

#     clear

}

       echo “Options :-“

result=`fun3 $1 $2 $3`

       echo

echo the result is $result

       echo ”     a) Add new CD”

[~/shell/function]# ./parameter.sh  1 2 3

       echo ”     f) Find CD”

the result is 6

       echo ”     c) Count the CDs and tracks in the catalog”

[~/shell/function]# ./parameter.sh  1 2

       if [ “$cdcatnum” != “” ];then #当前环境有对CD的操作

usage: ./parameter.sh a b c

              echo ”     l) List tracks on $cdtitle”

5.全局变量与局部变量

              echo ”     r) Remove $cdtitle”

默认条件下,在函数和shell主体中建立的变量都是全局变量,可以相互引用,当shell主体部分与函数部分拥有名字相同的变量时,可能会相互影响,例如:

              echo ”     u) Update track information for $cdtitle”

[~/shell/function]# cat ./variable.sh    

       fi

#!/bin/bash

       echo ”     q) Quit”

if [ $# -ne 3 ]

       echo

then

       echo -e “Please enter choice the press return: c”

    echo “usage: $0 a b c”

       read menu_choice #读入用户输入的操作选项

    exit

       return

fi

}

temp=5

#### set menu chocie function end ####

value=6

 

echo temp is: $temp

 

echo value is: $value

#### insert and add opration start  ####

fun3() {

insert_title()

    temp=`echo “scale=3;$1*$2*$3” | bc -ql`   

{

    result=$temp

       echo $* >> $title_file #将该函数的所有参数输入到文件

}

       return

fun3 $1 $2 $3

}

echo “the result is $result”

 

if [ `echo “$temp > $value” | bc -ql` -ne 0 ]

insert_track()

then 

{

    echo “temp is larger”

       echo $* >> $tracks_file

else

       return

    echo “temp is still smaller”

}

fi

 

[~/shell/function]# ./variable.sh  12 3 2

add_record_tracks()

temp is: 5

{

value is: 6

       echo “When no more tracks enter q”

the result is 72

       echo “Enter track information for this CD”

temp is larger

       cdtrack=1

在这种情况下,在函数内部最好使用局部变量,消除影响

       cdttitle=””

[~/shell/function]# cat ./variable.sh 

       while [ “$cdttitle” != “q” ]

#!/bin/bash

       do

if [ $# -ne 3 ]

              echo -e “Track $cdtrack, track title? c”

then

              read tmp

    echo “usage: $0 a b c”

              cdttitle=${tmp%%,*} #不能输入含有逗号的曲目名称

    exit

              if [ “$tmp” != “$cdttitle” ]; then

fi

                     echo “Sorry, no commas allowed”

temp=5

                     continue

value=6

              fi

echo temp is: $temp

              if [ -n “$cdttitle” ]; then

echo value is: $value

                     if [ “$cdttitle” != “q” ]; then

fun3() {

                            insert_track $cdcatnum,$cdtrack,$cdttitle
#调用函数,插入曲目信息

    local temp=`echo “scale=3;$1*$2*$3” | bc -ql`   

                     fi

    result=$temp

              else

}

                     cdtrack=$((cdtrack-1))
#当为q时,上次运行已经加1,所以做减一操作

fun3 $1 $2 $3

              fi

echo “the result is $result”

              cdtrack=$((cdtrack+1)) #曲目号码自动累加

if [ `echo “$temp > $value” | bc -ql` -ne 0 ]

       done

then 

}

    echo “temp is larger”

 

else

add_records()

    echo “temp is still smaller”

{

fi

       #Prompt for the initial infomation

[~/shell/function]# ./variable.sh  12 3 2

      

temp is: 5

       echo -e “Enter catalog name: c”

value is: 6

       read tmp

the result is 72

       cdcatnum=${tmp%%,*}

temp is still smaller

      

6.向函数传递数组变量:

       echo -e “Enter title: c”

[~/shell/function]# cat array.sh 

       read tmp

#!/bin/bash

       cdtitle=${tmp%%,*}

a=(11 12 13 14 15)

      

echo ${a[*]}

       echo -e “Enter type: c”

function array(){

       read tmp

  echo parameters :
“[email protected]” 

       cdtype=${tmp%%,*}

  local factorial=1

 

  for value in
“[email protected]”

       echo -e “Enter arrist or composer: c”

  do

       read tmp

    factorial=$[ $factorial * $value ]

       cdac=${tmp%%,*}

  done

 

  echo $factorial

       #Check that they want to enter the infomation    

}

       echo “About to add new entry”

array ${a[*]}

       echo “$cdcatnum, $cdtitle, $cdtype, $cdac” #显示变量的值的方式

[~/shell/function]# ./array.sh 

 

11 12 13 14 15

       #If confirmed then append it to the titles file

parameters : 11 12 13 14 15

       if get_confirm; then

360360

              insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
#添加CD信息

7.函数返回数组变量

              add_record_tracks
#给该CD添加曲目信息——需要一次性添加完毕

[~/shell/function]# cat array1.sh 

       else

#!/bin/bash

              remove_records #调用删除函数

a=(11 12 13 14 15)

       fi

function array(){

 

  echo parameters :
“[email protected]” 

       return

  local newarray=(`echo
“[email protected]”`)

}

  local element=”$#”

#### insert and add opration end  ####

  local i

 

  for (( i = 0; i < $element; i++ ))

 

  {

 

    newarray[$i]=$[ ${newarray[$i]} * 2 ]    

 

  }

#### list track start ####

  echo  new value:${newarray[*]}

list_tracks()

}

{

result=`array ${a[*]}`

        if [ “$cdcatnum” = “” ]; then

echo ${result[*]}

                echo “no CD selected yet”

[~/shell/function]# ./array1.sh 

                return

parameters : 11 12 13 14 15 new value:22 24 26 28 30

        else

Shell函数方法简介 linuxcCentOSDebianFreeBSD

                grep “^${cdcatnum},” $tracks_file > $temp_file

  1. 在shell文件内部定义函数并引用: [~/shell/function]# cat
    factorial.sh #!/bin/bash function factorial { factor…

 #在曲目文件中查找以CD号码开始以逗号结束的曲目信息,并将非此信息输出到临时文件

                set $(wc -l $temp_file)
#设置位置参数为统计曲目的个数信息

                num_tracks=$1 #即该位置参数是曲目的个数

              if [ “num_tracks” = “0” ]; then

                        echo “no tracks found for $cdtitle”

                else {

                        echo

                        echo “$cdtitle :-“

                        echo

                        cut -f 2- -d , $temp_file

#输出临时文件中曲目的后两项信息,即曲目号码,曲目名称

                        echo

              } | ${PAGER:-more} #分页显示

                fi

        fi

        get_return

        return

}

#### list tracks end ####

 

 

 

 

 

#### find CD start  ####

find_cd() #该函数需要一个参数,表示是否显示查询到的CD的曲目信息

{

       if [ “$1” = “n” ]; then

              asklist=n #不显示

       else

              asklist=y #显示

       fi

       cdcatnum=””

       echo -e “Enter a string to search for in the CD titles: c”

       read searchstr

       if [ “searchstr” = “” ]; then

              return 0

       fi

 

       grep “$searchstr” $title_file > $temp_file

#在CD文件中查找指定标题的信息存储到临时文件中

      

       set $(wc -l $temp_file)

       linesfound=$1

 

       case “$linesfound” in

              0)    echo “Sory, nothing found”

                     get_return

                     return 0

                     ;;

              1)    ;;

              2)    echo “Sorry, not unique”

                     echo “Found the following”

                     cat $temp_file

                     get_return

                     return 0

       esac

 

       IFS=”,” #修改默认选项,改为以逗号为分隔符

       read cdcatnum cdtitle cdtype cdac < $temp_file

#从临时文件以逗号为分隔符一次读入需要的CD信息选项

       IFS=” ” #恢复默认设置

 

       if [ -z “$cdcatnum” ]; then 
#变量cdcatnum是否是0,是0时返回true

              echo “Sorry, could not extract catalog field from
$temp_file”

              get_return

              return 0

       fi

 

       echo

       echo “Catalog number: $cdcatnum”

       echo “Title: $cdtitle”

       echo “Type: $cdtype”

       echo “Artist or Composer: $cdac”

       echo

       get_return

 

       if [ “$asklist” = “y” ]; then

              echo -e “View tracks for this CD? c”

              read x

              if [ “$x” = “y” ]; then

                     echo

                     list_tracks

                     echo

              fi

       fi

       return 1

}

#### find CD end ####

 

 

 

 

#### update CD start ####

update_cd()

{

       if [ -z “$cdcatnum” ]; then

              echo “You must select a CD first”

              find_cd n

       fi

       if [ -n “$cdcatnum” ]; then #变量cdcatnum非null吗,是返回true

              echo “Current tracks are :-“

              list_tracks

              echo

              echo “This will re-enter the tracks for $cdtitle”

              get_confirm && {

                                   grep -v “^${cdcatnum},” $tracks_file
> $temp_file

                                   mv $temp_file $tracks_file
#删除不需要的曲目信息,并更新曲目文件

                                   echo

                                   add_record_tracks #更新曲目信息

                             }

              fi

              return

}

#### update CD end ####

 

 

 

#### count CD start ####

count_cds()

{

       set $(wc -l $title_file)  #统计CD的个数

       num_titles=$1

       set $(wc -l $tracks_file) #统计曲目的个数

       num_tracks=$1

       echo “Found $num_titles CDs, with a total of $num_tracks
tracks”

       get_return

       return

}

#### count CD end ####

 

 

 

#### remove records start ####

remove_records()

{

       if [ -z “$cdcatnum” ]; then

              echo “You must select a CD first”

              find_cd n

       fi

       if [ -n “$cdcatnum” ]; then

              echo “You are about to delete $cdtitle”

              get_confirm && {

                                   grep -v “^${cdcatnum},” $title_file
> $temp_file

                                   mv $temp_file $title_file
#删除CD文件中不需要的CD信息

                                   grep -v “^${cdcatnum},” $tracks_file
> $temp_file

                                   mv $temp_file $tracks_file
#删除曲目文件中不需要的曲目信息

                                   cdcatnum=””

                                   echo “Entey removed”

                            }

       fi

       return

}

#### remove records end ####

 

 

 

#### main start ####

 

rm -rf $temp_file #先删除临时文件

if [ ! -f $title_file ]; then #判断CD文件非普通文件吗?是返回true

       touch $title_file #创建该CD文件

fi

if [ ! -f $tracks_file ]; then #同理判断曲目文件

       touch $tracks_file

fi

 

#Now the application proper

echo

echo

echo “Mini CD manager”

sleep 1

 

quit=n

while [ “$quit” != “y” ]

do

       set_menu_choice #显示选项操作项,并给变量menu_choice读入值

       case “$menu_choice” in

              a)    add_records;;

              r)     remove_records;;

              f)     find_cd y;;

              u)    update_cd;;

              c)    count_cds;;

              l)     list_tracks;;

              b)

                     echo

                     more $title_file

                     echo

                     get_return;;

              q | Q)      quit=y;;

              *)    echo “Sorry, choice not recognized”;;

       esac

done

 

#Tidy up and leave

rm -f $temp_file  #删除临时文件

echo “Finished”

exit 0

#### main end ####

 

=============================== 代码结束 ==============================

 

 

 

 

运行结果:

[root@localhost shell]# chmod +x cdmanager.sh   //给shell添加执行权限

[root@localhost shell]# ./cdmanager.sh          //运行该shell

 

 

Mini CD manager

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        q) Quit

 

Please enter choice the press return: a——输入a表示添加CD

Enter catalog name: CD88——输入CD88表示CD的编号

Enter title: myCD88——输入myCD88 表示CD88的标题

Enter type: liu_xing——输入liu_xing 表示CD88上的曲目类型

Enter arrist or composer: zhou_jie_lun——输入zhou_jie_lun
表示CD88的作曲家

About to add new entry

CD88, myCD88, liu_xing, zhou_jie_lun——显示添加信息,以便确认

Are you sure? y ——输入y表示确认无误

When no more tracks enter q

Enter track information for this CD

Track 1, track title? ju_hua_tai——输入CD88的曲目

Track 2, track title? huang_jin_jia——输入CD88的曲目

Track 3, track title? qian_li_zhi_wai——输入CD88的曲目

Track 4, track title? shuan_jie_gun——输入CD88的曲目

Track 5, track title? dong_feng_po——输入CD88的曲目

Track 6, track title? q——退出该CD88曲目的录入

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: l——输入l,显示当前CD的曲目信息

 

myCD88 :-

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0——输入一个返回值,0表示正常,非0表示异常

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: c——统计CD总数和曲目总数

Found 1 CDs, with a total of 5 tracks

Press return: 0——输入一个返回值,0表示正常,非0表示异常

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: a——添加CD信息

Enter catalog name: CD77

Enter title: myCD77

Enter type: gu_dian

Enter arrist or composer: yue_guang

About to add new entry

CD77, myCD77, gu_dian, yue_guang

Are you sure? q——输入参数错误

Please enter yes or no: yes——应该输入为yes或者no

When no more tracks enter q

Enter track information for this CD

Track 1, track title? yue_guang——添加当前CD的曲目信息

Track 2, track title? q——曲目信息添加结束

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: c——统计CD总个数和曲目总数

Found 2 CDs, with a total of 6 tracks

Press return: 0

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: f——查找指定CD标题的CD

Enter a string to search for in the CD titles:
myCD88——指定的CD标题为myCD88

 

Catalog number: CD88 ——显示该CD的信息

Title: myCD88

Type: liu_xing

Artist or Composer: zhou_jie_lun

 

Press return: 0

View tracks for this CD? y——显示该CD的曲目信息吗

 

 

myCD88 :-  ——该CD曲目信息的显示

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0

 

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: l——显示当前CD的曲目信息

 

myCD88 :-

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: u——更新当前的CD曲目信息

Current tracks are :-

 

myCD88 :-

 

1,ju_hua_tai

2,huang_jin_jia

3,qian_li_zhi_wai

4,shuan_jie_gun

5,dong_feng_po

 

Press return: 0

 

This will re-enter the tracks for myCD88

Are you sure? n——确认是否更新该CD

 

Cancelled

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD88

        r) Remove myCD88

        u) Update track information for myCD88

        q) Quit

 

Please enter choice the press return: f

Enter a string to search for in the CD titles: myCD77

 

Catalog number: CD77

Title: myCD77

Type: gu_dian

Artist or Composer: yue_guang

 

Press return: 0

View tracks for this CD? n——显示该CD的曲目信息吗

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: u

Current tracks are :-

 

myCD77 :-

 

1,yue_guang

 

Press return: 0

 

This will re-enter the tracks for myCD77

Are you sure? y——确认更新该CD的曲目信息(相当于该CD的曲目信息从新输入)

 

When no more tracks enter q

Enter track information for this CD

Track 1, track title? bei_duo_fen

Track 2, track title? shu_bo_te

Track 3, track title? q

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: l

 

myCD77 :-

 

1,bei_duo_fen

2,shu_bo_te

 

Press return: 0

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        l) List tracks on myCD77

        r) Remove myCD77

        u) Update track information for myCD77

        q) Quit

 

Please enter choice the press return: r——删除当前的CD

You are about to delete myCD77

Are you sure? y——确认删除当前CD

Entey removed

Options :-

 

        a) Add new CD

        f) Find CD

        c) Count the CDs and tracks in the catalog

        q) Quit

 

Please enter choice the press return: f

Enter a string to search for