$ XARGS 命令

xrags 的简单使用

linux 和 unix 的命令都带有参数, 有的命令可以直接使用标准输入 stdin 来作为参数, 如下实例:

# 查找 ck.log 中的 gomyck 关键字
$ cat /usr/share/gomyck/ck.log | grep gomyck
下面是 grep 命令的描述:
DESCRIPTION
   grep  searches  the  named  input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is
   given as file name) for lines containing a match to the given PATTERN.  By default, grep prints the matching lines.

   In addition, two variant programs egrep and fgrep are available.  egrep is the same as grep -E.  fgrep is  the  same
   as  grep -F.   Direct  invocation  as  either  egrep  or  fgrep  is  deprecated, but is provided to allow historical
   applications that rely on them to run unmodified.

可以看出, grep 是可以接受标准输入的命令

但是有些命令不能接受标准输入, 管道符失去了作用, 只能在命令行输入参数


#不好用
$ tail -n 10 ck.log | echo

上述命令无输出, 可以看出 echo 不接受标准输入作为参数

xargs 的使用

xargs 的作用是把标准输入转化成命令行参数, 所以一般来说, xargs 总是和管道符 | 一起搭配使用

$ tail -n 10 ck.log | xargs echo

使用方式: xargs [-options] [command]

xargs 默认的 command 是 echo

$ xargs 
$ hello world 
# ctrl + d
# echo
$ hello world
xargs 的描述
DESCRIPTION
     The xargs utility reads space, tab, newline and end-of-file delimited strings from
     the standard input and executes utility with the strings as arguments.

     Any arguments specified on the command line are given to utility upon each invoca-
     tion, followed by some number of the arguments read from the standard input of
     xargs.  The utility is repeatedly executed until standard input is exhausted.

     Spaces, tabs and newlines may be embedded in arguments using single (`` ' '') or
     double (``"'') quotes or backslashes (``\'').  Single quotes escape all non-single
     quote characters, excluding newlines, up to the matching single quote.  Double
     quotes escape all non-double quote characters, excluding newlines, up to the
     matching double quote.  Any single character, including newlines, may be escaped
     by a backslash.

xargs 默认约定以空格 tab 换行符 结束符来分隔字符串, 用来作为给定命令的参数, 也可以使用-d 参数来指定分隔字符


$ echo "gomyck1 gomyck2 gomyck3" | xargs mkdir

# echo -e 表示解释参数中的转译字符 \t
$ echo -e "gomyck1\tgomyck2\tgomyck3" | xargs -d "\t" mkdir

# xargs -p 会有用户确认过程, 输入 y 后才真正执行命令
$ echo -e "gomyck1\tgomyck2\tgomyck3" | xargs -d -p "\t" mkdir

# xargs -t 直接执行命令, 并打印
$ echo -e "gomyck1\tgomyck2\tgomyck3" | xargs -d -t "\t" mkdir

# find -print0 把查找结果按 null 分组, xargs -0 以 null 为分隔符
$ find / -name etc  -print0 | xargs -0 echo

# 打印文件, 删除文件  param 为占位符, sh -c 为执行命令
$ echo /usr/share/gomyck/ck.log | xargs -I param sh -c 'echo param; rm -rf param' 

其他知识点

在VMware Workstation中,默认有三个虚拟交换机VMnet0用于桥接,VMnet1用于仅主机网络,VMnet8用于NAT

个人博客 点击前往

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值