ZhangZhihui's Blog  

 

In bash we can pass the output of one command to the next one as an argument. We will cover multiple examples.

xargs is very useful for passing information between commands when chaining is used in Bash:

echo -e "Python\nJava" | xargs -I{} echo 'test me in' {}

-e enable interpretation of backslash escapes

-E disable interpretation of backslash escapes (default)

result:

test me in Python
test me in Java

Another example of passing output as argument with cURL:

cat urls.txt | xargs -I % curl http://example.com/%.json

 

zzh@ZZHPC:/zdata/Github/zimplebank$ ls -1 db/migration/*.down.sql | xargs -I{} ls -l {}
-rw-rw-r-- 1 zzh zzh 160 Dec 27 19:36 db/migration/000000_init_schema.down.sql
-rw-rw-r-- 1 zzh zzh 40 Dec 27 19:36 db/migration/000001_add_sessions.down.sql
-rw-rw-r-- 1 zzh zzh 105 Dec 27 19:37 db/migration/000002_add_verification_emails.down.sql
zzh@ZZHPC:/zdata/Github/zimplebank$ ls -1 db/migration/*.down.sql | xargs -I % ls -l %
-rw-rw-r-- 1 zzh zzh 160 Dec 27 19:36 db/migration/000000_init_schema.down.sql
-rw-rw-r-- 1 zzh zzh 40 Dec 27 19:36 db/migration/000001_add_sessions.down.sql
-rw-rw-r-- 1 zzh zzh 105 Dec 27 19:37 db/migration/000002_add_verification_emails.down.sql

 

复制代码
zzh@ZZHPC:/zdata/Github/zimplebank$ sh << EOF
> ls -1 db/migration/*.up.sql | xargs -I{} psql postgresql://root:aaa@localhost:5432/zimple_bank?sslmode=disable -f {}
> EOF
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
COMMENT
COMMENT
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
ALTER TABLE
复制代码

 

initschema4githubtest:
	ls -1 db/migration/*.up.sql | xargs -I{} psql "$(DB_URL)" -f {}

 

posted on   ZhangZhihuiAAA  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示