暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux中的xargs命令妙用

watson 2025-03-09
29

【导读】


  xargs可以读入stdin的数据,并且以空格符或者换行符作为识别符。将stdin的数据分割成为参数。

  注意:如果有一些文件名或者其他意义的名词内含有空格符的时候,xargs就可能误判了。


一 xargs命令的参数介绍

[root@achao achao]# xargs --help Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.
Mandatory arguments to long options are mandatory for short options too.Non-mandatory arguments are indicated by [square brackets] -0, --null Items are separated by a null, not whitespace. Disables quote and backslash processing -a, --arg-file=FILE Read arguments from FILE, not standard input -d, --delimiter=CHARACTER Input items are separated by CHARACTER, not by blank space. Disables quote and backslash processing -E END If END occurs as a line of input, the rest of the input is ignored. -e [END], --eof[=END] Equivalent to -E END if END is specified. Otherwise, there is no end-of-file string --help Print a summary of the options to xargs. -I R same as --replace=R (R must be specified) -i,--replace=[R] Replace R in initial arguments with names read from standard input. If R is unspecified, assume {} -L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per command line -l Use at most one nonblank input line per command line -n, --max-args=MAX-ARGS Use at most MAX-ARGS arguments per command line -P, --max-procs=MAX-PROCS Run up to max-procs processes at a time -p, --interactive Prompt before running commands --process-slot-var=VAR Set environment variable VAR in child processes -r, --no-run-if-empty If there are no arguments, run no command. If this option is not given, COMMAND will be run at least once. -s, --max-chars=MAX-CHARS Limit commands to MAX-CHARS at most --show-limits Show limits on command-line length. -t, --verbose Print commands before executing them --version Print the version number  -x, --exit                   Exit if the size (see -s) is exceeded
复制

  这里的参数很多,就不展开说啦,感兴趣的可以上看英文或者自己搜索解决。


二 参数-n后边接次数,每次command执行时,使用几个参数

[root@achao achao]# cut -d ":" -f 1 /etc/passwd  | head -n 3rootbindaemon[root@achao achao]# cut -d ":" -f 1 /etc/passwd  | head -n 3 | xargs -n 1 iduid=0(root) gid=0(root) groups=0(root)uid=1(bin) gid=1(bin) groups=1(bin)uid=2(daemon) gid=2(daemon) groups=2(daemon)
复制


三 参数-p执行命令的时候会咨询使用者的意见

[root@achao achao]# cut -d ":" -f 1 /etc/passwd  | head -n 3 | xargs -p -n 1 idid root ?...yid bin ?...uid=0(root) gid=0(root) groups=0(root)yid daemon ?...uid=1(bin) gid=1(bin) groups=1(bin)yuid=2(daemon) gid=2(daemon) groups=2(daemon)
复制

  个人觉得生产环境中这个参数是一点用途也没有,各位看官大佬们瞅一眼就可以。


四 导读中的注意事项设计案列解答

[root@achao achao]# df -Th | grep -v Type | awk -F ' ' '{print $2}'xfsdevtmpfstmpfstmpfstmpfsxfstmpfsiso9660ext4[root@achao achao]# df -Th | grep -v Type | awk -F ' ' '{print $2}' | sort devtmpfsext4iso9660tmpfstmpfstmpfstmpfsxfsxfs[root@achao achao]# df -Th | grep -v Type | awk -F ' ' '{print $2}' | sort | uniqdevtmpfsext4iso9660tmpfsxfs
复制

  这里得到了操作系统中含有的所有文件系统的格式。接下来进行高速驾驶,系好安全带,我们开始。

[root@achao achao]# df -Th | grep -v Type | awk -F ' ' '{print $2}' | sort | uniq | xargsdevtmpfs ext4 iso9660 tmpfs xfs[root@achao achao]# file_system=$(df -Th | grep -v Type | awk -F ' ' '{print $2}' | sort | uniq | xargs)[root@achao achao]# for i in ${file_system};do> echo ${i}> donedevtmpfsext4iso9660tmpfsxfs
复制

    使用shell脚本进行测试

[root@achao achao]# cat test.sh #!/bin/bashfile_system=$(df -Th | grep -v Type | awk -F ' ' '{print $2}' | sort | uniq | xargs)for i in ${file_system};do{  echo ${i}}done[root@achao achao]# sh test.sh devtmpfs ext4 iso9660 tmpfs xfs[root@achao achao]# 
复制

       我们发现,结果竟然不一样。在生产环境中遇到过,将列转行,会看成一个整体的字符串。这里就不是我们想要的结果。

  导论中说过,xargs可以直接读取列的数据,因此不需要将列转行的,但是有些操作系统是可以的,空格依然是支持的。有些操作系统就不支持,因此,保稳的措施是直接采用列来操作,可保万无一失,这个是在工作中总结数来的经验,有感兴趣的可以留言讨论。


四 xargs提供该命令使用的标准输入

[root@achao achao]# find /etc -name "*.sh" | xargs ls -l-rwxr-xr-x. 1 root root  409 Apr 11  2018 /etc/dhcp/dhclient.d/chrony.sh-rw-r--r--. 1 root root  594 Apr 11  2018 /etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh-rwxr-xr-x. 1 root root 1676 Apr 11  2018 /etc/kernel/postinst.d/51-dracut-rescue-postinst.sh-rw-r--r--. 1 root root  841 Apr 11  2018 /etc/profile.d/256term.sh-rw-r--r--. 1 root root 1348 Apr 27  2018 /etc/profile.d/abrt-console-notification.sh-rw-r--r--. 1 root root  660 Jun 10  2014 /etc/profile.d/bash_completion.sh-rw-r--r--. 1 root root  201 Mar 25  2017 /etc/profile.d/colorgrep.sh-rw-r--r--. 1 root root 1606 Apr 11  2018 /etc/profile.d/colorls.sh-rw-r--r--. 1 root root  373 Apr 11  2018 /etc/profile.d/flatpak.sh-rw-r--r--. 1 root root 2703 Apr 11  2018 /etc/profile.d/lang.sh-rw-r--r--. 1 root root  121 Jul 31  2015 /etc/profile.d/less.sh-rw-r--r--. 1 root root 1202 Aug  6  2017 /etc/profile.d/PackageKit.sh-rw-r--r--. 1 root root  269 Apr 11  2018 /etc/profile.d/vim.sh-rw-r--r--. 1 root root 2092 Sep  4  2017 /etc/profile.d/vte.sh-rw-r--r--. 1 root root  169 Jan 28  2014 /etc/profile.d/which2.sh-rwxr-xr-x. 1 root root 5861 Apr 11  2018 /etc/smartmontools/smartd_warning.sh-rwxr-xr-x. 1 root root  621 Apr 11  2018 /etc/X11/xinit/xinitrc.d/00-start-message-bus.sh-rwxr-xr-x. 1 root root  384 Apr 11  2018 /etc/X11/xinit/xinitrc.d/10-qt5-check-opengl2.sh-rwxr-xr-x. 1 root root 3969 Jun 10  2014 /etc/X11/xinit/xinitrc.d/50-xinput.sh-rwxr-xr-x. 1 root root  543 Apr 11  2018 /etc/X11/xinit/xinitrc.d/localuser.sh-rwxr-xr-x. 1 root root  842 May  4  2018 /etc/X11/xinit/xinitrc.d/zz-liveinst.sh
复制

  这不,一下子就把etc目录下的所有shell脚本查找出来了。当然啦,也可以使用如下方式的。

[root@achao achao]# ls -l $(find /etc/ -name "*.sh")-rwxr-xr-x. 1 root root  409 Apr 11  2018 /etc/dhcp/dhclient.d/chrony.sh-rw-r--r--. 1 root root  594 Apr 11  2018 /etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh-rwxr-xr-x. 1 root root 1676 Apr 11  2018 /etc/kernel/postinst.d/51-dracut-rescue-postinst.sh-rw-r--r--. 1 root root  841 Apr 11  2018 /etc/profile.d/256term.sh-rw-r--r--. 1 root root 1348 Apr 27  2018 /etc/profile.d/abrt-console-notification.sh-rw-r--r--. 1 root root  660 Jun 10  2014 /etc/profile.d/bash_completion.sh-rw-r--r--. 1 root root  201 Mar 25  2017 /etc/profile.d/colorgrep.sh-rw-r--r--. 1 root root 1606 Apr 11  2018 /etc/profile.d/colorls.sh-rw-r--r--. 1 root root  373 Apr 11  2018 /etc/profile.d/flatpak.sh-rw-r--r--. 1 root root 2703 Apr 11  2018 /etc/profile.d/lang.sh-rw-r--r--. 1 root root  121 Jul 31  2015 /etc/profile.d/less.sh-rw-r--r--. 1 root root 1202 Aug  6  2017 /etc/profile.d/PackageKit.sh-rw-r--r--. 1 root root  269 Apr 11  2018 /etc/profile.d/vim.sh-rw-r--r--. 1 root root 2092 Sep  4  2017 /etc/profile.d/vte.sh-rw-r--r--. 1 root root  169 Jan 28  2014 /etc/profile.d/which2.sh-rwxr-xr-x. 1 root root 5861 Apr 11  2018 /etc/smartmontools/smartd_warning.sh-rwxr-xr-x. 1 root root  621 Apr 11  2018 /etc/X11/xinit/xinitrc.d/00-start-message-bus.sh-rwxr-xr-x. 1 root root  384 Apr 11  2018 /etc/X11/xinit/xinitrc.d/10-qt5-check-opengl2.sh-rwxr-xr-x. 1 root root 3969 Jun 10  2014 /etc/X11/xinit/xinitrc.d/50-xinput.sh-rwxr-xr-x. 1 root root  543 Apr 11  2018 /etc/X11/xinit/xinitrc.d/localuser.sh-rwxr-xr-x. 1 root root  842 May  4  2018 /etc/X11/xinit/xinitrc.d/zz-liveinst.sh
复制

 效果是一样的。


五 总结


  总是,不管是黑猫白猫,抓住老鼠的就是好猫,在前期阶段,我们首先想到的是如何实现功能,然后在是优化,提高性能。举一个很简单的例子。

[root@achao achao]# cat /etc/passwd | head -1root:x:0:0:root:/root:/bin/bash[root@achao achao]# head -1 /etc/passwdroot:x:0:0:root:/root:/bin/bash
复制

  都实现了相同的功能,但是那一条命令效率更加的高效的,这个时候只有你积累到一定程度,自然而然就有了答案。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论