[问]bash脚本for循环参数问题
发表于 : 2011-11-07 13:19
自己看shell 13问.还有for循环里in后面跟$@很常见,但是不知道怎么解释
代码: 全选
#!/bin/bash
for cname in $@
do
whatis $cname >> a.txt
done
sort a.txt
代码: 全选
chmod +foo.sh
./foo.sh `ls /bin/`
arch (1) - print machine architecture
awk (1p) - pattern scanning and processing language
bash (1) - GNU Bourne-Again SHell
bashbug (1) - report a bug in bash
bunzip2 (1) - a block-sorting file compressor, v1.0.6
bzcat (1) - decompresses files to stdout
bzip2 (1) - a block-sorting file compressor, v1.0.6
bzip2recover (1) - recovers data from damaged bzip2 files
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
chgrp (1) - change group ownership
chgrp (1p) - change the file group ownership
chmod (1) - change file mode bits
chmod (1p) - change the file modes
chmod (2) - change permissions of a file
chmod (3p) - change mode of a file
chown (1) - change file owner and group
chown (1p) - change the file ownership
chown (2) - change ownership of a file
chown (3p) - change owner and group of a file
cp (1) - copy files and directories
cp (1p) - copy files
cpio (1) - copy files to and from archives
cpio (5) - format of cpio archive files
cut (1p) - cut out selected fields of each line of a file
cut (1) - remove sections from each line of files
dash (1) - command interpreter (shell)
date (1) - print or set the system date and time
date (1p) - write the date and time
dd (1) - convert and copy a file
dd (1p) - convert and copy a file
df (1p) - report free disk space
df (1) - report file system disk space usage
dir (1) - list directory contents
dircolors (1) - color setup for ls
dmesg (1) - print or control the kernel ring buffer
dnsdomainname (1) - show the system's DNS domain name
domainname (1) - show or set the system's NIS/YP domain name
du (1) - estimate file space usage
du (1p) - estimate file space usage
echo (1) - display a line of text
echo (1p) - write arguments to standard output
echo (3x) - curses input options
ed (1p) - edit text
ed (1) - text editor
egrep (1) - print lines matching a pattern
false (1) - do nothing, unsuccessfully
false (1p) - return false value
fgrep (1) - print lines matching a pattern
findmnt (8) - find a filesystem
gawk (1) - pattern scanning and processing language
grep (1) - print lines matching a pattern
grep (1p) - search a file for a pattern
groups (1) - display current group names
gunzip (1) - compress or expand files
gzip (1) - compress or expand files
hostname (1) - show or set the system's host name
hostname (7) - hostname resolution description
install (1) - copy files and set attributes
keyctl (1) - Key management facility control
keyctl (2) - Manipulate the kernel's key management facility
kill (1p) - terminate or signal processes
kill (1) - send a signal to a process
kill (2) - send signal to a process
kill (3p) - send a signal to a process or a group of processes
less (1) - opposite of more
less (3perl) - perl pragma to request less of something
ln (1) - make links between files
ln (1p) - link files
loadkeys (1) - load keyboard translation tables
login (1) - begin session on the system
login (3) - write utmp and wtmp entries
ls (1) - list directory contents
ls (1p) - list directory contents
lsblk (8) - list block devices
lsmod (8) - program to show the status of modules in the Linux Kernel
mbchk (1) - check the format of a Multiboot kernel
mkdir (1) - make directories
mkdir (1p) - make directories
mkdir (2) - create a directory
mkdir (3p) - make a directory
mkfifo (1) - make FIFOs (named pipes)
mkfifo (1p) - make FIFO special files
mkfifo (3) - make a FIFO special file (a named pipe)
mkfifo (3p) - make a FIFO special file
mknod (1) - make block or character special files
mknod (2) - create a special or ordinary file
mknod (3p) - make a directory, a special file, or a regular file
more (1) - file perusal filter for crt viewing
more (1p) - display files on a page-by-page basis
mount (2) - mount file system
mount (8) - mount a filesystem
mountpoint (1) - see if a directory is a mountpoint
mv (1) - move (rename) files
mv (1p) - move files
netstat (8) - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
ntfs-3g (8) - Third Generation Read/Write NTFS Driver
pidof (8) - find the process ID of a running program.
ping6 (8) - send ICMP ECHO_REQUEST to network hosts
ping (8) - send ICMP ECHO_REQUEST to network hosts
ps (1p) - report process status
ps (1) - report a snapshot of the current processes.
pwd (1p) - return working directory name
pwd (1) - print name of current/working directory
pwd (n) - Return the absolute path of the current working directory
readlink (1) - print value of a symbolic link or canonical file name
readlink (2) - read value of a symbolic link
readlink (3p) - read the contents of a symbolic link
red (1) - text editor
rm (1p) - remove directory entries
rm (1) - remove files or directories
rmdir (1p) - remove directories
rmdir (1) - remove empty directories
rmdir (2) - delete a directory
rmdir (3p) - remove a directory
sed (1p) - stream editor
sed (1) - stream editor for filtering and transforming text
sh (1p) - shell, the standard command language interpreter
shred (1) - overwrite a file to hide its contents, and optionally delete it
sleep (1) - delay for a specified amount of time
sleep (1p) - suspend execution for an interval
sleep (3p) - suspend execution for an interval of time
sleep (3) - Sleep for the specified number of seconds
stty (1) - change and print terminal line settings
stty (1p) - set the options for a terminal
stty (2) - unimplemented system calls
su (1) - run a shell with substitute user and group IDs
sync (1) - flush file system buffers
sync (2) - commit buffer cache to disk
sync (3p) - schedule file system updates
sync (8) - synchronize data on disk with memory
tar (1) - The GNU version of the tar archiving utility
tar (5) - format of tape archive files
tc-red (8) - Random Early Detection
touch (1) - change file timestamps
touch (1p) - change file access and modification times
tput (1) - initialize a terminal or query terminfo database
tput (1p) - change terminal characteristics
tr (1p) - translate characters
tr (1) - translate or delete characters
traceroute6 (8) - traces path to a network host
traceroute (8) - print the route packets take to network host
true (1) - do nothing, successfully
true (1p) - return true value
umount (2) - unmount file system
umount (8) - unmount file systems
uname (1p) - return system name
uname (1) - print system information
uname (2) - get name and information about current kernel
uname (3p) - get the name of the current system
uncompress (1p) - expand compressed data
vdir (1) - list directory contents
zcat (1) - compress or expand files
zcat (1p) - expand and concatenate data
zsh (1) - the Z shell
那么也就是说,这个脚本的作用就是收集某个文件夹下所有指令程序的帮助文件?那个$@是指代命令行输入的命令吗naturalaw 写了:自己看shell 13问.还有for循环里in后面跟$@很常见,但是不知道怎么解释
好吧.明白怎么回事了.
这样吧.save as foo.sh代码: 全选
#!/bin/bash for cname in $@ do whatis $cname >> a.txt done sort a.txt
so the output is代码: 全选
chmod +foo.sh ./foo.sh `ls /bin/`
arch (1) - print machine architecture
awk (1p) - pattern scanning and processing language
bash (1) - GNU Bourne-Again SHell
bashbug (1) - report a bug in bash
bunzip2 (1) - a block-sorting file compressor, v1.0.6
bzcat (1) - decompresses files to stdout
bzip2 (1) - a block-sorting file compressor, v1.0.6
bzip2recover (1) - recovers data from damaged bzip2 files
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
chgrp (1) - change group ownership
chgrp (1p) - change the file group ownership
chmod (1) - change file mode bits
chmod (1p) - change the file modes
chmod (2) - change permissions of a file
chmod (3p) - change mode of a file
chown (1) - change file owner and group
chown (1p) - change the file ownership
chown (2) - change ownership of a file
chown (3p) - change owner and group of a file
cp (1) - copy files and directories
cp (1p) - copy files
cpio (1) - copy files to and from archives
cpio (5) - format of cpio archive files
cut (1p) - cut out selected fields of each line of a file
cut (1) - remove sections from each line of files
dash (1) - command interpreter (shell)
date (1) - print or set the system date and time
date (1p) - write the date and time
dd (1) - convert and copy a file
dd (1p) - convert and copy a file
df (1p) - report free disk space
df (1) - report file system disk space usage
dir (1) - list directory contents
dircolors (1) - color setup for ls
dmesg (1) - print or control the kernel ring buffer
dnsdomainname (1) - show the system's DNS domain name
domainname (1) - show or set the system's NIS/YP domain name
du (1) - estimate file space usage
du (1p) - estimate file space usage
echo (1) - display a line of text
echo (1p) - write arguments to standard output
echo (3x) - curses input options
ed (1p) - edit text
ed (1) - text editor
egrep (1) - print lines matching a pattern
false (1) - do nothing, unsuccessfully
false (1p) - return false value
fgrep (1) - print lines matching a pattern
findmnt (8) - find a filesystem
gawk (1) - pattern scanning and processing language
grep (1) - print lines matching a pattern
grep (1p) - search a file for a pattern
groups (1) - display current group names
gunzip (1) - compress or expand files
gzip (1) - compress or expand files
hostname (1) - show or set the system's host name
hostname (7) - hostname resolution description
install (1) - copy files and set attributes
keyctl (1) - Key management facility control
keyctl (2) - Manipulate the kernel's key management facility
kill (1p) - terminate or signal processes
kill (1) - send a signal to a process
kill (2) - send signal to a process
kill (3p) - send a signal to a process or a group of processes
less (1) - opposite of more
less (3perl) - perl pragma to request less of something
ln (1) - make links between files
ln (1p) - link files
loadkeys (1) - load keyboard translation tables
login (1) - begin session on the system
login (3) - write utmp and wtmp entries
ls (1) - list directory contents
ls (1p) - list directory contents
lsblk (8) - list block devices
lsmod (8) - program to show the status of modules in the Linux Kernel
mbchk (1) - check the format of a Multiboot kernel
mkdir (1) - make directories
mkdir (1p) - make directories
mkdir (2) - create a directory
mkdir (3p) - make a directory
mkfifo (1) - make FIFOs (named pipes)
mkfifo (1p) - make FIFO special files
mkfifo (3) - make a FIFO special file (a named pipe)
mkfifo (3p) - make a FIFO special file
mknod (1) - make block or character special files
mknod (2) - create a special or ordinary file
mknod (3p) - make a directory, a special file, or a regular file
more (1) - file perusal filter for crt viewing
more (1p) - display files on a page-by-page basis
mount (2) - mount file system
mount (8) - mount a filesystem
mountpoint (1) - see if a directory is a mountpoint
mv (1) - move (rename) files
mv (1p) - move files
netstat (8) - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
ntfs-3g (8) - Third Generation Read/Write NTFS Driver
pidof (8) - find the process ID of a running program.
ping6 (8) - send ICMP ECHO_REQUEST to network hosts
ping (8) - send ICMP ECHO_REQUEST to network hosts
ps (1p) - report process status
ps (1) - report a snapshot of the current processes.
pwd (1p) - return working directory name
pwd (1) - print name of current/working directory
pwd (n) - Return the absolute path of the current working directory
readlink (1) - print value of a symbolic link or canonical file name
readlink (2) - read value of a symbolic link
readlink (3p) - read the contents of a symbolic link
red (1) - text editor
rm (1p) - remove directory entries
rm (1) - remove files or directories
rmdir (1p) - remove directories
rmdir (1) - remove empty directories
rmdir (2) - delete a directory
rmdir (3p) - remove a directory
sed (1p) - stream editor
sed (1) - stream editor for filtering and transforming text
sh (1p) - shell, the standard command language interpreter
shred (1) - overwrite a file to hide its contents, and optionally delete it
sleep (1) - delay for a specified amount of time
sleep (1p) - suspend execution for an interval
sleep (3p) - suspend execution for an interval of time
sleep (3) - Sleep for the specified number of seconds
stty (1) - change and print terminal line settings
stty (1p) - set the options for a terminal
stty (2) - unimplemented system calls
su (1) - run a shell with substitute user and group IDs
sync (1) - flush file system buffers
sync (2) - commit buffer cache to disk
sync (3p) - schedule file system updates
sync (8) - synchronize data on disk with memory
tar (1) - The GNU version of the tar archiving utility
tar (5) - format of tape archive files
tc-red (8) - Random Early Detection
touch (1) - change file timestamps
touch (1p) - change file access and modification times
tput (1) - initialize a terminal or query terminfo database
tput (1p) - change terminal characteristics
tr (1p) - translate characters
tr (1) - translate or delete characters
traceroute6 (8) - traces path to a network host
traceroute (8) - print the route packets take to network host
true (1) - do nothing, successfully
true (1p) - return true value
umount (2) - unmount file system
umount (8) - unmount file systems
uname (1p) - return system name
uname (1) - print system information
uname (2) - get name and information about current kernel
uname (3p) - get the name of the current system
uncompress (1p) - expand compressed data
vdir (1) - list directory contents
zcat (1) - compress or expand files
zcat (1p) - expand and concatenate data
zsh (1) - the Z shell
我這裏只是舉個例子.那么也就是说,这个脚本的作用就是收集某个文件夹下所有指令程序的帮助文件?那个$@是指代命令行输入的命令吗
$@表示全部參數whatis (1) - display manual page descriptions
代码: 全选
#!/bin/bash
for cname in $@
do
whatis $cname >> a.txt
done
sort a.txt