Linux shell学习Demo(Linux程序设计第四版)

1、参考:http://blog.csdn.net/u011410413/article/details/43909567
2、文本格式:UNIX(建议Windows使用Notepad++编辑轻松设置)
3、树莓派和windows之间文件传输
3.1、运行如下命令安装samba软件
sudo apt-get install samba samba-common-bin
3.2、安装完成后,修改配置文件/etc/samba/smb.conf
sudo nano /etc/samba/smb.conf
配置每个用户可以读写自己的 home 目录,在“[homes]”节中,把 “read only = yes” 改为 “read only = no”。
3.3、重启samba服务
sudo /etc/init.d/samba restart
3.4、添加默认用户pi到samba
sudo smbpasswd -a pi
设置密码:raspberry
3.5、访问树莓派文件
使用文件浏览器打开ip地址\192.1682.105\pi (树莓派IP建议设置为静态IP),输入用户密码,则可以访问树莓派home目录。
3、笔记

#!/bin/sh

# Very simple example shell script for managing a CD collection.
# Copyright (C) 1996-99 Wrox Press.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.

# This program is distributed in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.
# 675 Mass Ave, Cambridge, MA 02139, USA.

# The first thing to do is to ensure that some global variables that we'll be using
# throughout the script are set up. We set the title and track files and a temporary file.
# We also trap Ctrl-C, so our temporary file is removed if the user interrupts the script.

menu_choice=""
current_cd=""
#CD唱片信息:CD目录编号,标题,曲目类型,作曲家
title_file="title.cdb"
#曲目信息:CD目录编号,曲目编号,曲名
tracks_file="tracks.cdb"
#$$当前进程的id号
temp_file=/tmp/cdb.$$
#捕获Ctrl-C,因此如果用户中断脚本,我们的临时文件将被删除。
trap 'rm -f $temp_file' EXIT
echo $temp_file

# Now we define our functions, so that the script, executing from the top line, can find
# all the function definitions before we attempt to call any of them for the first time.
# To avoid rewriting the same code in several places, the first two functions are simple
# utilities.

get_return() {
  echo -e "Press return \c"
  read x
  return 0
}

get_confirm() {
  echo -e "Are you sure? \c"
  while true
  do
    read x
    case "$x" in
      y | yes | Y | Yes | YES ) 
        return 0;;
      n | no  | N | No  | NO ) 
        echo 
        echo "Cancelled"
        return 1;;
      *) echo "Please enter yes or no" ;;
    esac
  done
}

# Here, we come to the main menu function, set_menu_choice.
# The contents of the menu vary dynamically, with extra options being added if a CD entry
# has been selected. Note that echo -e may not be portable to some shells.

set_menu_choice() {
  clear
  echo "Options :-"
  echo
  echo "   a) Add new CD"
  echo "   f) Find CD"
  echo "   c) Count the CDs and tracks in the catalog"
  if [ "$cdcatnum" != "" ]; then
    echo "   l) List tracks on $cdtitle"
    echo "   r) Remove $cdtitle"
    echo "   u) Update track information for $cdtitle"
  fi
  echo "   q) Quit"
  echo
  echo -e "Please enter choice then press return \c"
  read menu_choice
  return
}

# Two more very short functions, insert_title and insert_track for adding to the database files.
# Though some people hate one-liners like these, they help make other functions clearer
# They are followed by the larger add_record_track function that uses them.
# This function uses pattern matching to ensure no commas are entered (since we're using commas
# as a field separator), and also arithmetic operations to increment the current track number
# as tracks are entered.

insert_title() {
#$*以一个单字符串显示所有向脚本传递的参数
#此处可判断添加CD目录编号不可与原来的重复
  tmp=$*
  grep "^${tmp%%,*}," $title_file > $temp_file
  #搜索CD目录编号的行数,执行set之后$*的值已经改变
  set $(wc -l $temp_file)
  linesfound=$1
  case "$linesfound" in
  0)    echo $tmp >> $title_file
        return 0
        ;;
  *)    echo "Sorry, CD is existed."
        ;;
  esac
  return 1
}

insert_track() {
  echo $* >> $tracks_file
  return
}

add_record_tracks() {
  echo "Enter track information for this CD"
  echo "When no more tracks enter q"
  cdtrack=1
  cdttitle=""
  while [ "$cdttitle" != "q" ]
  do
      echo -e "Track $cdtrack, track title? \c"
      read tmp
      #%%,*就是把第一个,号之前的数据拿出来,而%,*就是把最后一个,之前的数据拿出来
      cdttitle=${tmp%%,*}
      if [ "$tmp" != "$cdttitle" ]; then
        echo "Sorry, no commas allowed"
        continue
      fi
      #当串的长度大于0时为真(串非空)
      if [ -n "$cdttitle" ] ; then
        if [ "$cdttitle" != "q" ]; then
          insert_track $cdcatnum,$cdtrack,$cdttitle
        fi
      else
        cdtrack=$((cdtrack-1))
      fi
    cdtrack=$((cdtrack+1))
  done
}

# The add_records function allows entry of the main CD information for a new CD.

add_records() {
  # Prompt for the initial information

  echo -e "Enter catalog name \c"
  read tmp
  cdcatnum=${tmp%%,*}

  echo -e "Enter title \c"
  read tmp
  cdtitle=${tmp%%,*}

  echo -e "Enter type \c"
  read tmp
  cdtype=${tmp%%,*}

  echo -e "Enter artist/composer \c"
  read tmp
  cdac=${tmp%%,*}

  # Check that they want to enter the information

  echo About to add new entry
  echo "$cdcatnum $cdtitle $cdtype $cdac"

  # If confirmed then append it to the titles file

  if get_confirm ; then
    insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
    if test $? -eq 0; then
        add_record_tracks
    else
        get_return
    fi
  else
    remove_records
  fi 
  return
}

# The find_cd function searches for the catalog name text in the CD title file, using the
# grep command. We need to know how many times the string was found, but grep only returns
# a value telling us if it matched zero times or many. To get around this, we store the
# output in a file, which will have one line per match, then count the lines in the file.
# The word count command, wc, has whitespace in its output, separating the number of lines,
# words and characters in the file. We use the $(wc -l $temp_file) notation to extract the
# first parameter from the output to set the linesfound variable. If we wanted another,
# later parameter we would use the set command to set the shell's parameter variables to
# the command output.
# We change the IFS (Internal Field Separator) to a , (comma), so we can separate the
# comma-delimited fields. An alternative command is cut.

find_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
  #$searchstr为空,则""赋值给该变量
  if [ "$searchstr" = "" ]; then
    return 0
  fi

  grep "$searchstr" $title_file > $temp_file
  #搜索包括字段的行数 
  set $(wc -l $temp_file)
  linesfound=$1

  case "$linesfound" in
  0)    echo "Sorry, nothing found"
        get_return
        return 0
        ;;
  1)    ;;
  2)    echo "Sorry, not unique."
        echo "Found the following"
        cat $temp_file
        get_return
        return 0
  esac

  #linesfound等于1时显示
  IFS=","
  read cdcatnum cdtitle cdtype cdac < $temp_file
  IFS=" "

  if [ -z "$cdcatnum" ]; then
    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/Composer: $cdac
  echo
  get_return

  #显示对应CD的曲目信息
  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
}

# update_cd allows us to re-enter information for a CD. Notice that we search (grep)
# for lines that start (^) with the $cdcatnum followed by a ,, and that we need to wrap
# the expansion of $cdcatnum in {} so we can search for a , with no whitespace between
# it and the catalogue number. This function also uses {} to enclose multiple statements
# to be executed if get_confirm returns true.

update_cd() {
  if [ -z "$cdcatnum" ]; then
    echo "You must select a CD first"
    find_cd n
  fi
  if [ -n "$cdcatnum" ]; then
    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
}

# count_cds gives us a quick count of the contents of our database.

count_cds() {
  set $(wc -l $title_file)
  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
}

# remove_records strips entries from the database files, using grep -v to remove all
# matching strings. Notice we must use a temporary file.
# If we tried to do this,
# grep -v "^$cdcatnum" > $title_file
# the $title_file would be set to empty by the > output redirection before the grep
# had chance to execute, so grep would read from an empty file.

remove_records() {
  #当串的长度为0时为真(空串) 
  if [ -z "$cdcatnum" ]; then
    echo You must select a CD first
    find_cd n
  fi
  #当串的长度大于0时为真(串非空)
  if [ -n "$cdcatnum" ]; then
    echo "You are about to delete $cdtitle"
    get_confirm && {
      #查找文件名中包含"开头CD目录编号"的文件中不包含"开头CD目录编号"的行
      grep -v "^${cdcatnum}," $title_file > $temp_file
      #把临时文件名修改成原来文件名
      mv $temp_file $title_file
      grep -v "^${cdcatnum}," $tracks_file > $temp_file
      mv $temp_file $tracks_file
      cdcatnum=""
      echo Entry removed
    }
    get_return
  fi
  return
}

# List_tracks again uses grep to extract the lines we want, cut to access the fields
# we want and then more to provide a paginated output. If you consider how many lines
# of C code it would take to re-implement these 20-odd lines of code, you'll appreciate
# how powerful a tool the shell can be.

list_tracks() {
  if [ "$cdcatnum" = "" ]; then
    echo no CD selected yet
    return
  else
    grep "^${cdcatnum}," $tracks_file > $temp_file
    num_tracks=$(wc -l $temp_file)
    if [ "$num_tracks" = "0" ]; then
      echo no tracks found for $cdtitle
    else { 
      echo
      echo "$cdtitle :-"
      echo 
      #显示字段2之后的所有字段,以,分隔号
      cut -f 2- -d , $temp_file 
      echo 
    } | ${PAGER:-more}
    # 如果当前屏幕页面设置已被定义则执行当前页面设置,如限制了输出内容。不然的话,就将启用more这个命令来定义屏幕显示结果
    fi
  fi
  get_return
  return
}

# Now all the functions have been defined, we can enter the main routine.
# The first few lines simply get the files into a known state, then we call the menu
# function, set_menu_choice, and act on the output.
# When quit is selected, we delete the temporary file, write a message and exit
# with a successful completion condition.

rm -f $temp_file
if [ ! -f $title_file ]; then
  touch $title_file
fi
if [ ! -f $tracks_file ]; then
  touch $tracks_file
fi

# Now the application proper

clear
echo
echo
echo "Mini CD manager"
sleep 1

quit=n
while [ "$quit" != "y" ];
do
  set_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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值