简体   繁体   English

ZSH/Shell 变量赋值/使用

[英]ZSH/Shell variable assignment/usage

I use ZSH for my terminal shell, and whilst I've written several functions to automate specific tasks, I've never really attempted anything that requires the functionality I'm after at the moment.我将 ZSH 用于我的终端 shell,虽然我编写了几个函数来自动执行特定任务,但我从未真正尝试过任何需要我目前所追求的功能的东西。

I've recently re-written a blog using Jekyll and I want to automate the production of blog posts and finally the uploading of the newly produced files to my server using something like scp.我最近使用 Jekyll 重写了一个博客,我想自动生成博客文章,最后使用 scp 之类的东西将新生成的文件上传到我的服务器。

I'm slightly confused about the variable bindings/usage in ZSH;我对 ZSH 中的变量绑定/使用有点困惑; for example:例如:

DATE= date +'20%y-%m-%d'
echo $DATE

correctly outputs 2011-08-23 as I'd expect.正如我所料,正确输出 2011-08-23 。

But when I try:但是当我尝试时:

DATE= date +'20%y-%m-%d'
FILE= "~/path/to/_posts/$DATE-$1.markdown"
echo $FILE

It outputs:它输出:

2011-08-23
blog.sh: line 4: ~/path/to/_posts/-.markdown: No such file or directory

And when run with what I'd be wanting the blog title to be (ignoring the fact the string needs to be manipulated to make it more url friendly and that the route path/to doesn't exist)当以我想要的博客标题运行时(忽略需要对字符串进行操作以使其对 url 更友好以及路由路径/到不存在的事实)

ie blog "blog title", outputs:即博客“博客标题”,输出:

2011-08-23
blog.sh: line 4: ~/path/to/_posts/-blog title.markdown: No such file or directory

Why is $DATE printing above the call to print $FILE rather than the string being included in $FILE?为什么在打印 $FILE 的调用上方打印 $DATE 而不是 $FILE 中包含的字符串?

Two things are going wrong here.这里有两件事出了问题。


Firstly, your first snippet is not doing what I think you think it is.首先,你的第一个片段没有做我认为你认为的那样。 Try removing the second line, the echo .尝试删除第二行echo It still prints the date, right?它仍然打印日期,对吗? Because this:因为这:

DATE= date +'20%y-%m-%d'

Is not a variable assignment - it's an invocation of date with an auxiliary environment variable (the general syntax is VAR_NAME=VAR_VALUE COMMAND ).不是变量赋值 - 它是使用辅助环境变量调用date (一般语法是VAR_NAME=VAR_VALUE COMMAND )。 You mean this:你是这个意思:

DATE=$(date +'20%y-%m-%d')

Your second snippet will still fail, but differently.您的第二个片段仍然会失败,但会有所不同。 Again, you're using the invoke-with-environment syntax instead of assignment.同样,您使用的是invoke-with-environment语法而不是赋值。 You mean:你的意思是:

# note the lack of a space after the equals sign
FILE="~/path/to/_posts/$DATE-$1.markdown"

I think that should do the trick.我认为这应该可以解决问题。


Disclaimer免责声明

While I know bash very well, I only started using zsh recently;虽然我非常了解 bash,但我最近才开始使用zsh there may be zshisms at work here that I'm not aware of.这里可能有我不知道的zshisms

Learn about what a shell calls 'expansion'.了解 shell 所称的“扩展”。 There are several kinds, performed in a particular order:有几种,按特定顺序执行:

The order of word expansion is as follows:扩词顺序如下:

  1. tilde expansion波浪扩展
  2. parameter expansion参数扩展
  3. command substitution命令替换
  4. arithmetic expansion算术展开
  5. pathname expansion , unless set -f is in effect路径名扩展,除非set -f生效
  6. quote removal , always performed last报价删除,总是最后执行

Note that tilde expansion is only performed when the tilde is not quoted;请注意,波浪号扩展仅在波浪号不被引用时执行; viz.:即:

$ FILE="~/.zshrc"
$ echo $FILE
~/.zshrc
$ FILE=~./zshrc
$ echo $FILE
/home/user42/.zshrc

And there must be no spaces around the = in variable assignments.变量赋值中的=周围不能有空格。

Since you asked in a comment where to learn shell programming, there are several options:由于您在评论中询问在哪里学习 shell 编程,所以有几个选择:

  • Read the shell's manual page man zsh阅读 shell 的手册页man zsh
  • Read the specification of the POSIX shell, http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html , especially if you want to run your scripts on different operating systems (and you will find yourself in that situation one fine day!)阅读 POSIX shell 的规范, http ://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html,特别是如果你想在不同的操作系统上运行你的脚本(你发现自己在那种情况下)美好的一天!)
  • Read books about shell programming.阅读有关 shell 编程的书籍。
  • Hang out in the usenet newsgroup comp.unix.shell where a lot of shell wizards answer questions在 usenet 新闻组 comp.unix.shell 闲逛,那里有很多 shell 向导回答问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM