Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116896
  • 博文数量: 73
  • 博客积分: 66
  • 博客等级: 民兵
  • 技术积分: 497
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-22 14:59
文章分类

全部博文(73)

文章存档

2015年(65)

2013年(5)

2012年(3)

我的朋友

分类: LINUX

2015-02-10 17:48:26

点击(此处)折叠或打开

  1. TEMP_FILE=/tmp/printfile.txt
  2. trap "rm $TEMP_FILE; echo 'exising and removed the $TEMP_FILE';exit 0" \
  3. SIGHUP SIGINT SIGTERM
  4. pr $1 > $TEMP_FILE
  5. echo -n "print file? [y/n]: "
  6. read
  7. if [ "$REPLY" = "y" ]; then
  8. echo "print file"
  9. fi
  10. rm $TEMP_FILE

    A clean_up Function

    While the trap command has solved the problem, we can see that it has some limitations. Most importantly, it will only accept a single string containing the command to be performed when the signal is received. You could get clever and use ";" and put multiple commands in the string to get more complex behavior, but frankly, it's ugly. A better way would be to create a function that is called when you want to perform any actions at the end of your script. In my scripts, I call this function clean_up.

    #!/bin/bash
    
    # Program to print a text file with headers and footers
    
    TEMP_FILE=/tmp/printfile.txt
    
    clean_up() {
    
    	# Perform program exit housekeeping
    	rm $TEMP_FILE exit } trap clean_up SIGHUP SIGINT SIGTERM
    
    pr $1 > $TEMP_FILE echo -n "Print file? [y/n]: " read if [ "$REPLY" = "y" ]; then lpr $TEMP_FILE fi clean_up 
阅读(639) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~