Printf使用Bash在空格处分割字符串 [英] Printf splits a string at spaces using Bash

查看:86
本文介绍了Printf使用Bash在空格处分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash中的printf函数遇到了一些麻烦. 我写了一个小脚本,上面传递了一个名称和两个字母(例如"sh","py","ht"),并在当前工作目录中创建了一个名为"name.extension"的文件. 例如,如果我执行seed test py,则会在当前工作目录中使用shebang #!/usr/bin/python3创建一个名为test.py的文件.

I'm having some troubles with the printf function in bash. I wrote a little script on which I pass a name and two letters (such as "sh", "py", "ht") and it creates a file in the current working directory named "name.extension". For instance, if I execute seed test py a file named test.py is created in the current working dir with the shebang #!/usr/bin/python3.

到目前为止,还不错,没什么花哨的:我正在学习shell脚本,我认为这可能是测试到目前为止所学知识的简单练习. 问题是当我要创建HTML文件时.这是我使用的功能:

So far, so good, nothing fancy: I'm learning shell scripting and I thought this could be a simple exercise to test the knowledge gained so far. The problem is when I want to create an HTML file. This is the function that I use:

creaHtml(){
        head='<!--DOCTYPE html-->\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t</head>\n\t<body>\n\t</body>\n</html>'
        percorso=$CARTELLA_CORRENTE/$NOME_FILE.html
        printf $head>>$percorso
        chmod 755 $percorso

}

例如,如果我运行seed test ht,则会调用正确的函数(creaHtml),将创建test.html,但是如果我尝试对其进行研究,则只会看到:

If I run, for instance, seed test ht the correct function (creaHtml) is called, test.html is created but if I try to look into it I only see:

<!--DOCTYPE

没有别的. 这是该函数的踪迹:

And nothing else. This is the trace for that function:

[sviluppo:~/bin]$ seed test ht
+ creaHtml
+ head='<!--DOCTYPE html-->\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t</head>\n\t<body>\n\t</body>\n</html>'
+ percorso=/home/sviluppo/bin/test.html
+ printf '<!--DOCTYPE' 'html-->\n<html>\n\t<head>\n\t\t<meta' 'charset=\"UTF-8\">\n\t</head>\n\t<body>\n\t</body>\n</html>'
+ chmod 755 /home/sviluppo/bin/test.html
+ set +x

但是,如果我尝试从终端运行printf '<!--DOCTYPE html-->\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t</head>\n\t<body>\n\t</body>\n</html>',我会看到正确的输出:HTML文件的框架"以缩进和所有内容整齐地显示.我在这里想念什么?

However, if I try to run printf '<!--DOCTYPE html-->\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t</head>\n\t<body>\n\t</body>\n</html>' from the terminal, I see the correct output: the "skeleton" of an HTML file neatly displayed with indentation and everything. What am I missing here?

推荐答案

尝试使用echo -e而不是printf. printf用于打印格式化的字符串.由于您没有用引号保护$head,因此bash会拆分字符串以形成命令.第一个单词(在第一个空格之前)形成格式字符串.其余只是您未指定要打印的内容的参数.

Try echo -e instead of printf. printf is for printing formatted strings. Since you didn't protect $head with quotes, bash splits the string to form the command. The first word (before first white space) forms the format string. The rest are just arguments for things you didn't specify to print.

echo -e "$head" > "$percorso"

-e将您的\n评估为换行符.我将您的>>更改为>,因为它看起来像您希望将其作为整个文件,而不是附加到您可能拥有的任何现有文件中.

The -e evaluates your \n into newlines. I changed your >> to > since it looks like you want this to be the whole file, rather than append to any existing file you might have.

您必须小心bash中的引号.一件事可以变成很多事情.这实际上使它更强大,但是对于人们的学习却可能造成混淆.注意,我也将文件名"$percorso"放在了双引号中.这将评估变量并确保将其作为一件事结束.如果使用单引号,它将是一个单词,但不会被评估.与Python不同,单引号和双引号之间有很大的区别.

You have to be careful with quotes in bash. One thing can become many things. This actually makes it more powerful, but it can be confusing for people learning. Notice that I also put the file name "$percorso" in double quotes too. This evaluates the variable and makes sure that it ends up as one thing. If you use single quotes, it will be one word, but not evaluated. Unlike Python, there is a big difference between single and double quotes.

如果您要使用@cepner指出的printf兼容性,请务必将其引用:

If you want to use printf for compatibility as @chepner pointed out, just be sure to quote it:

printf "$head" > "$percorso"

实际上这反而要简单得多.

Actually that is much simpler anyway.

这篇关于Printf使用Bash在空格处分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆