xargs的简单使用

284 阅读1分钟
1. 无参数
// hello world
echo "hello world" | xargs
默认以echo作为输出,即等价于echo "hello world" | xargs echo
默认以空格作为界定符,换行和空白将被空格取代
默认是单行显示

2. -d指定界定符
// hello world
echo "helloXworld" | xargs -dX

3. -n指定多行输出
// h e l
// l o
echo "h e l l o" | xargs -n3

4. -I扩展替换
// PING www.google.com (172.217.160.100) 56(84) bytes of data.
// 64 bytes from 172.217.160.100: icmp_seq=1 ttl=128 time=1006 ms
// 64 bytes from 172.217.160.100: icmp_seq=4 ttl=128 time=957 ms
// ...
echo "www.google.com" | xargs -I {} ping {}

5. -t打印xargs执行的命令
// ping www.baidu.com
// PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
// 64 bytes from 180.101.49.11: icmp_seq=1 ttl=128 time=9.15 ms
// 64 bytes from 180.101.49.11: icmp_seq=2 ttl=128 time=12.0 ms
// ...
echo "www.baidu.com" | xargs -I {} -t ping {}

6. -p弹出确认执行选项
// ping www.baidu.com ?...
echo "www.baidu.com" | xargs -I {} -t -p ping {}