首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bash将输出解析为变量

Bash将输出解析为变量
EN

Stack Overflow用户
提问于 2012-04-17 16:20:41
回答 4查看 836关注 0票数 3

我在试着把我的照片分成肖像和风景。我想出了一个打印jpeg大小尺寸的命令:

代码语言:javascript
运行
复制
identify -format '%w %h\n' 1234.jpg 
1067 1600

如果我在bash脚本中使用它将所有的风景图片移到另一个文件夹中,我希望它是这样的

代码语言:javascript
运行
复制
#!/bin/bash
# loop through file (this is psuedo code!!)
for f in ~/pictures/
do
 # Get the dimensions (this is the bit I have an issue with)
 identify -format '%w %h\n' $f | awk # how do I get the width and height?
 if $width > $hieght
  mv ~/pictures/$f ~/pictures/landscape/$f
 fi
done

我一直在看awk手册页,但我似乎找不到语法。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-04-17 16:32:16

你不需要AWK。这样做:

代码语言:javascript
运行
复制
identify -format '%w %h\n' $f | while read width height
do
    if [[ $width -gt $height ]]
    then
        mv ~/pictures/$f ~/pictures/landscape/$f
    fi
done
票数 3
EN

Stack Overflow用户

发布于 2012-04-17 16:27:02

您可以使用array

代码语言:javascript
运行
复制
# WxH is a array which contains (W, H)
WxH=($(identify -format '%w %h\n' $f))
width=${WxH[0]}
height=${WxH[1]}
票数 4
EN

Stack Overflow用户

发布于 2012-04-17 16:27:12

代码语言:javascript
运行
复制
format=`identify -format '%w %h\n' $f`;
height=`echo $format | awk '{print $1}'`;
width=`echo $format | awk '{print $2}'`;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10187617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档