[英]Insert newline character at index in .bash
I'm taking an introductory course to bash at my university and am working on a little MotD script that uses a json-object grabbed from an API using curl. 我正在上大学的入门课程,正在研究一个MotD脚本,该脚本使用使用curl从API抓取的json对象。
I want to make absolutely certain that you understand that this is NOT an assignment, but something I'm playing around with to learn more about how to script with bash. 我想绝对确定您了解这不是一项任务,而是我正在研究的东西,以了解有关如何使用bash编写脚本的更多信息。
I've found myself stuck with what could possibly be a very simply issue; 我发现自己陷入了一个非常简单的问题; I want to insert a new line ('
\\n
') on a specific index if the 'quote' value of my json-object is too long (in this case on index 80). 如果我的json对象的'quote'值太长(在本例中为索引80),我想在特定索引上插入新行('
\\n
')。
I've been following a bunch of SO threads and this is my current solution: 我一直在跟踪一堆SO线程,这是我当前的解决方案:
#!/bin/bash
json_object=$(curl -s 'http://quotes.stormconsultancy.co.uk/random.json')
quote=$(echo ${json_object} | jq .quote | sed -e 's/^"//' -e 's/"$//')
author=$(echo ${json_object} | jq .author)
count=${#quote}
echo $quote
echo $author
echo "wc: $count"
if((count > 80));
then
quote=${quote:0:80}\n${quote:80:(count - 80)}
else
echo "lower"
fi
printf "$quote"
The current output I receive from the printf
is the first word of the quote, whereas if I have an echo
before trying to do the string-manipulation I get the entire quote. 我从
printf
接收到的当前输出是引号的第一个单词,而如果在尝试进行字符串操作之前有echo
,则会得到整个引号。
I'm sorry if it's not following best practice or anything, but I'm an absolute beginner using both vi
and bash
. 很抱歉,如果它没有遵循最佳实践或其他任何东西,但是我绝对是同时使用
vi
和bash
初学者。
I'd be very happy with any sort of advice. 我会对任何建议感到非常满意。 :)
:)
EDIT: 编辑:
Sample output: 样本输出:
$ ./json.bash
$ ./json.bash
You should name a variable using the same care with which you name a first-born child.
命名变量的命名方式应与头胎孩子的命名方式相同。
"James O. Coplien"
“詹姆斯·科普林”
86
86
higher
更高
You should name a variable using the same care with which you name a first-born nchild.
您应该像命名第一个nchild一样谨慎地命名变量。
You can just use a single line bash
command to achieve this, 您可以使用单行
bash
命令来实现此目的,
string="You should name a variable using the same care with which you name a first-born child."
(( "${#string}" > 80 )) && printf "%s\n" "${string:0:80}"$'\n'"${string:80}" || printf "%s\n" "$string"
You should name a variable using the same care with which you name a first-born
child.
(and) for an input line less than 80
charaacters (和)输入少于
80
字符的行
string="You should name a variable using the same care"
(( "${#string}" > 80 )) && printf "%s\n" "${string:0:80}"$'\n'"${string:80}" || printf "%s\n" "$string"
You should name a variable using the same care
An explanation, 一个解释,
(( "${#string}" > 80 )) && printf "%s\n" "${string:0:80}"$'\n'"${string:80}" || printf "%s\n" "$string"
# The syntax is a indirect implementation of ternary operator as bash doesn't
# directly support it.
#
# (( "${#string}" > 80 )) will return a success/fail depending upon the length
# of the string variable and if it is greater than 80, the command after && is
# executed and if it fails the command after || is executed
#
# "${string:0:80}"$'\n'"${string:80}"
# A parameter expansion syntax for sub-string extraction.
#
# ${PARAMETER:OFFSET}
#
# ${PARAMETER:OFFSET:LENGTH}
#
# This one can expand only a part of a parameter's value, given a position
# to start and maybe a length. If LENGTH is omitted, the parameter will be
# expanded up to the end of the string. If LENGTH is negative, it's taken as
# a second offset into the string, counting from the end of the string.
#
# So in our example we basically extract the characters from position 0 to 80
# insert a new-line and append the rest of the string
#
# The $'\n' syntax allows to include all escape sequence characters be
# included, in this case just the new line character.
Not really in the original question, but adding some extra code to @Inian great answer to allow not to break in the middle of a word, but rather at the last white space in ${string:0:80}
: 并不是最初的问题,而是在@Inian好的答案中添加了一些额外的代码,以使它们不会在单词中间中断,而是在
${string:0:80}
的最后一个空格处中断:
#!/usr/bin/env bash
string="You should really name a variable using the same care with which you name a first-born child."
if (( "${#string}" > 80 )); then
maxstring="${string:0:80}"
lastspace="${maxstring##*\ }"
breakat="$((${#maxstring} - ${#lastspace}))"
printf "%s\n" $"${string:0:${breakat}}"$'\n'"${string:${breakat}}"
else
printf "%s\n" "$string"
fi
maxstring=${string:0:80}
: maxstring=${string:0:80}
:
Let's get the first 80 characters of the quote. 让我们获取报价的前80个字符。
lastspace=${maxstring##*\\ }
: lastspace=${maxstring##*\\ }
:
Deletes longest match of *\\
(white space is escaped) from front of $maxstring
, ${lastspace}
will be the remaining string from last white space until end of the string. 从
$maxstring
删除*\\
最长匹配项(转义空格), ${lastspace}
将是最后一个空格到字符串末尾的剩余字符串。
breakat="$((${#maxstring} - ${#lastspace}))"
: breakat="$((${#maxstring} - ${#lastspace}))"
:
Subtract the length of ${lastspace}
with the length of ${maxstring}
to get the last index of the white space from ${maxstring}
. 减去的长度
${lastspace}
与长度${maxstring}
从中获取的白色空间的最后一个索引${maxstring}
。 This is the index where \\n
will be inserted. 这是将插入
\\n
的索引。
Example output with "hard" break at character 80: 示例输出在字符80处“硬”中断:
You should really name a variable using the same care with which you name a firs
t-born child.
Example output with a "soft" break at the closest white space from character 80: 在与字符80最接近的空白处带有“软”中断的示例输出:
You should really name a variable using the same care with which you name a
first-born child.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.