免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3068 | 回复: 13
打印 上一主题 下一主题

UNIX SHELL 实例精解学习贴(每天2例) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-02 22:35 |只看该作者 |倒序浏览
2009-03-02

ex 13.04
# /etc/profile
# Systemwide environment and startup programs
# Functions and aliases go in /etc/bashrc

PATH="$PATH:/usr/X11R6/bin" #设置SHELL查找命令的路径
PS1="[\u@\h \W]\\$ " #这里设置\u 是用户名 \h 是机器名 \W为当前目录 [username@chinaunix shell_dir]$

ulimit -c 1000000 #如果此文件CORE DUMP的时候最大的CORE文件大小为1000,000bytes
if [ `id -gn` = `id -un` -a `id -u` -gt 14 ]; then #id -gn 取groupname,id -un 取username,id-u 取用户的ID。
        umask 002
else
        umask 022
fi

USER=`id -un`
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"

HOSTNAME=`/bin/hostname`
HISTSIZE=1000
HISTFILESIZE=1000
export PATH PS1 HOSTNAME HISTSIZE HISTFILESIZE USER LOGNAME MAIL

for i in /etc/profile.d/*.sh ; do
        if [ -x $i ]; then
                . $i
        fi
done # 对/etc/profile.d/*.sh下面的sh文件遍历,全部执行一边,如果这个文件的权限为可执行的话

unset i #


ex 13.05
# .bash_profile
# The file is sourced by bash only when the user logs on.
#source让此文件在当前进程内执行
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi#-f 在这里判断~/.bashrc时候是一个文件

# User-specific environment and startup programs

PATH=$PATHHOME/bin
ENV=$HOME/.bashrc     # or BASH_ENV=$HOME/.bashrc
USERNAME="root"
export USERNAME ENV PATH
mesg n #The mesg command is executed with the n option, disallowing others to write to the terminal
if [ $TERM = linux ]
then
startx    # Start the X Window system
fi


Note1:注意if then fi的两种写法。
Note2:此贴用于个人学习,欢迎讨论并指出错误。


  1. #!/bin/bash
  2. # Gnu bash versions 2.x
  3. # The Party Program--Invitations to friends from the
  4. # "guest" file
  5. #guestfile=~/shell/guests
  6. guestfile=./guests
  7. if [[ ! -e "$guestfile" ]]
  8. then
  9. printf "${guestfile##*/} non-existent"
  10. exit 1
  11. fi
  12. export PLACE="Sarotini's"
  13. (( Time=$(date +%H) + 1 ))
  14. declare -a foods=(cheese crackers shrimp drinks '"hot dogs"'
  15.                   sandwiches)  
  16.    declare -i  n=0
  17. for person in $(cat $guestfile)
  18. do
  19. if  [[ $person == root ]]
  20. then
  21.   continue
  22. else
  23.   # Start of here document
  24.   mail -v -s "Party" $person <<- FINIS
  25.   Hi $person! Please join me at $PLACE for a party!
  26.   Meet me at $Time o'clock.
  27.   I'll bring the ice cream. Would you please bring
  28.   ${foods[$n]} and anything else you would like to eat?
  29.   Let me know if you can make it.
  30.          Hope to see you soon.
  31.                           Your pal,
  32.                           ellie@$(hostname)
  33.   FINIS
  34.   n=n+1  
  35.   if (( ${#foods[*]} ==  $n ))
  36.   then
  37.      declare -a foods=(cheese crackers shrimp drinks
  38.                        `"hot dogs"` sandwiches)
  39.             n=0
  40.   fi
  41. fi
  42. done  
  43. printf "Bye..."
复制代码












学习结果展示贴:持续改进。。。。
#!/bin/bash

#####################Main Part##############################

if [ id -un!="Cuser" ];then
                checkCuser()
else
    umask 002
    sourceAll()
fi

mesg n
ulimit -c 1000000

if [ $SHELL!="Bash" ]
then
bash    # Change to Bash
fi

###########################################################


#####################Function set #########################
function checkCuser() {
echo "lease change to Cuser,use \"su -\""
}

function sourceAll() {
for i in ./scripts/*.sh; do
        if [ -x $i ];then
            . $i
        fi
done
}

###########################################################

[ 本帖最后由 wxws2002 于 2009-3-3 21:38 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-03-02 23:19 |只看该作者
学习了,谢谢,这就是传说中的UNIX SHELL 实例精解书中的实例???

论坛徽章:
0
3 [报告]
发表于 2009-03-03 00:32 |只看该作者

回复 #1 wxws2002 的帖子

受用

论坛徽章:
0
4 [报告]
发表于 2009-03-03 07:11 |只看该作者
看得有点晕,排版有待加强

论坛徽章:
9
2015亚冠之阿尔纳斯尔
日期:2015-09-10 16:21:162015亚冠之塔什干火车头
日期:2015-07-01 16:23:022015年亚洲杯之巴勒斯坦
日期:2015-04-20 17:19:46子鼠
日期:2014-11-13 09:51:26未羊
日期:2014-08-28 18:13:36技术图书徽章
日期:2014-02-21 09:30:15酉鸡
日期:2014-01-14 11:12:49天蝎座
日期:2013-12-09 17:56:53平安夜徽章
日期:2015-12-26 00:06:30
5 [报告]
发表于 2009-03-03 08:26 |只看该作者
不错,实际的例子,
/etc/init.d/下面也有很多

论坛徽章:
0
6 [报告]
发表于 2009-03-03 10:20 |只看该作者
汗啊,我今天刚好看到这本书的这一页。。。
难怪看得很眼熟

论坛徽章:
0
7 [报告]
发表于 2009-03-04 22:48 |只看该作者

今天学了一下Sed

例子很多,但是书里面的例子还是挺浅的,还是需要多在版上学习学习啊!
一直有个疑问

sed -n  '/[0-9][0-9]$/' datafile 为什么回的结果为空呢?

datafile:
northwest        NW        Joel Craig        3.0        .98        3        4
western        WE        Sharon Kelly        5.3        .97        5        23
southwest        SW        Chris Foster        2.7        .8        2        18
southern        SO        May Chin        5.1        .95        4        15
southeast        SE        Derek Johnson        4.0        .7        4        17
eastern        EA        Susan Beal        4.4        .84        5        20
northeast        NE        TJ Nichols        5.1        .94        3        13
north        NO        Val Shultz        4.5        .89        5        9
central        CT        Sheri Watson        5.7        .94        5        13


我的想法是末尾为2个数字的应该都能匹配上啊?

论坛徽章:
0
8 [报告]
发表于 2009-03-05 07:20 |只看该作者

回复 #7 wxws2002 的帖子

你cat -A datafile

论坛徽章:
0
9 [报告]
发表于 2009-03-05 09:27 |只看该作者
cat -A datafile
northwest^INW^ICharles Main^I^I3.0^I.98^I3^I34^M$
western^I^IWE^ISharon Gray^I^I5.3^I.97^I5^I23^M$
southwest^ISW^ILewis Dalsass^I^I2.7^I.8^I2^I18^M$
southern^ISO^ISuan Gray^I^I5.1^I.95^I4^I15^M$
southeast ^ISE^IPatricia Hemenway^I4.0^I.7^I4^I17^M$
eastern^I^IEA^ITB Savage^I^I4.4^I.84^I5^I20^M$
northeast ^INE^IAM Main Jr.^I^I5.1^I.94^I3^I13^M$
north^I^INO^IMargot Weber^I^I4.5^I.89^I5^I 9^M$
central^I^ICT ^IAnn Stephens^I^I5.7^I.94^I5^I13^M$



sed -n  '/[0-9][0-9].$/p' datafile 就可以,难道是^M?问题是用编辑器是删不掉这个换行符的。


cat -A data
1234 4567 2334$
2333 3333$
21122 a v ds ss aa   

自己变了个文件,没有^M,看来这个是windows下编辑造成的。

[ 本帖最后由 wxws2002 于 2009-3-5 09:38 编辑 ]

论坛徽章:
1
处女座
日期:2014-12-23 17:59:27
10 [报告]
发表于 2009-03-05 11:15 |只看该作者
sed -n  '/[0-9][0-9]$/' datafile

这样的话, 不管你什么样的模式 结果都为空.
我这里甚至报错.
sed: -e expression #1, char 13: missing command
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP