linuxshell命令之代码块重定向
代码块重定向是指对标准输⼊或者标准输出的重定向只在代码块内有效。
可以重定向的代码块可以是while、until、for等循环结构,也可以是
if/then测试结构。甚⾄可以是函数。
代码块输⼊重定向符号是<,输出重定向符号是>.
while循环重定向的例⼦
rewhile.sh
#!/bin/bash
#将lt /etc的结果写到loggg⽂件中
ls /etc > loggg
#搜索loggg⽂件中与rc.d所匹配的⾏,输出匹配⾏的⾏数
while [ "$filename" != "rc0.d" ] #当不匹配时,执⾏while循环体
do
#按⾏读妈loggg内容
read filename
echo "filename = $filename"
let "count += 1"
echo "count = $count"
done < loggg
echo "$count times read"
#测试循环体外⾯的标准输⼊是否被重定向
echo -n "-----Pls. Input Data:-----"
read test
echo $test
执⾏: ./rewhile.sh
filename = acpi
count = 1
filename = f
count = 2
filename = alternatives
count = 3
filename = anacrontab
count = 4
filename = apache2
count = 5
filename = f
count = 6
filename = apm
count = 7
filename = apparmor
count = 8
filename = apparmor.d
count = 9
filename = apport
count = 10
filename = f
count = 11
filename = apt
count = 12
filename = aptdaemon
count = 13
filename = at-spi2
count = 14
filename = avahi
count = 15
filename = bash.bashrc
filename = bindresvport.blacklist count = 19
filename = binfmt.d
count = 20
filename = bluetooth
count = 21
filename = bonobo-activation
count = 22
filename = brlapi.key
count = 23
filename = brltty
count = 24
filename = f
count = 25
filename = ca-certificates
count = 26
filename = f
count = 27
filename = f.dpkg-old count = 28
filename = calendar
count = 29
filename = chatscripts
count = 30
filename = compizconfig
count = 31
filename = console-setup
count = 32
filename = cracklib
count = 33
filename = cron.d
count = 34
filename = cron.daily
count = 35
filename = cron.hourly
count = 36
filename = hly
count = 37
filename = crontab
count = 38
filename = cron.weekly
count = 39
filename = cups
count = 40
filename = cupshelpers
count = 41
filename = dbus-1
count = 42
filename = dconf
count = 43
filename = f
count = 44
filename = debian_version
count = 45
filename = default
count = 46
filename = f
count = 47
filename = depmod.d
count = 48
filename = dhcp
count = 49
filename = dictionaries-common
filename = drirc
count = 54
filename = emacs
count = 55
filename = environment count = 56
filename = exports
count = 57
filename = firefox
count = 58
filename = fonts
count = 59
filename = fstab
count = 60
filename = ftpusers
count = 61
filename = f
count = 62
filename = f count = 63
filename = f
count = 64
filename = gconf
count = 65
filename = gdb
count = 66
filename = ghostscript count = 67
filename = gnome
count = 68
filename = gnome-app-install count = 69
filename = gnome-vfs-2.0 count = 70
filename = groff
count = 71
filename = group
count = 72
filename = group-
count = 73
filename = grub.d
count = 74
filename = gshadow
count = 75
filename = gshadow-
count = 76
filename = gss
count = 77
filename = gtk-2.0
count = 78
filename = gtk-3.0
count = 79
filename = guest-session count = 80
filename = f count = 81
filename = f
count = 82
filename = hostname
count = 83
filename = hosts
filename = f count = 88
filename = ifplugd
count = 89
filename = iftab
count = 90
filename = ImageMagick-6 count = 91
filename = f
count = 92
filename = init
count = 93
filename = init.d
count = 94
filename = initramfs-tools count = 95
filename = inputrc
count = 96
filename = insserv
count = 97
filename = f count = 98
filename = f.d count = 99
filename = iproute2
count = 100
filename = issue
count = 101
filename = issue.dpkg-old count = 102
filename = issue
count = 103
filename = issue.dpkg-old count = 104
filename = java-7-openjdk count = 105
filename = kbd
count = 106
filename = kermit
count = 107
filename = kernel
count = 108
filename = f count = 109
filename = f count = 110
filename = ldap
count = 111
filename = ld.so.cache count = 112
filename = f
count = 113
filename = f.d count = 114
filename = legal
count = 115
filename = f
count = 116
filename = f count = 117
filename = libnl-3
filename = lightdm
count = 121
filename = lighttpd
count = 122
filename = lintianrc
count = 123
filename = locale.alias
count = 124
filename =
count = 125
filename = localtime
count = 126
filename = logcheck
count = 127
filename = login.defs
count = 128
filename = f count = 129
filename = logrotate.d
count = 130
filename = lsb-release
count = 131
filename = lsb-release.dpkg-old count = 132
filename = f
shell代码
count = 133
filename = machine-id
count = 134
filename = magic
count = 135
filename = magic.mime
count = 136
filename = mailcap
count = 137
filename = der count = 138
filename = fig count = 139
filename = mercurial
count = 140
filename = pes
count = 141
filename = minicom
count = 142
filename = f
count = 143
filename = modprobe.d
count = 144
filename = modules
count = 145
filename = modules-load.d count = 146
filename = mtab
count = 147
filename = f
count = 148
filename = mysql
count = 149
filename = nanorc
count = 150
filename = netconfig
count = 151
filename = network

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。