我有以下代码。
read -p "Enter a word: " word
case $word in
[aeiou]* | [AEIOU]*)
echo "The word begins with a vowel." ;;
[0-9]*)
echo "The word begins with a digit." ;;
*[0-9])
echo "The word ends with a digit." ;;
[aeiou]* && [AEIOU]* && *[0-9])
echo "The word begins with vowel and ends with a digit." ;;
????)
echo "You entered a four letter word." ;;
*)
echo "I don't know what you've entered," ;;
esac
Run Code Online (Sandbox Code Playgroud)
当我运行这个:
Enter a word: …Run Code Online (Sandbox Code Playgroud) 我有以下文本文件。
banana
apple
juice
mango
something
Run Code Online (Sandbox Code Playgroud)
我正在搜索 pattern juice,我想以相反的顺序从该匹配模式中找到第二行(即匹配模式上方的 2 行)并将其替换为coconut.
预期输出:
coconut
apple
juice
mango
something
Run Code Online (Sandbox Code Playgroud)
我尝试了以下操作,但它只是删除了上面两行,而不是我正在寻找的那一行。
tac foo.txt |sed '/juice/I,+2 d' |tac
mango
something
Run Code Online (Sandbox Code Playgroud)
我认为调整上面的脚本可以完成这项工作,但我不确定。
注意:匹配不会再次发生,也不需要完全匹配(也就是说,匹配也可以在长行中找到)。匹配应该区分大小写。
有没有办法递归查找并删除所有播放时间少于 3 分钟的音频文件(MP3 文件)?
考虑我混合有多种格式文件(例如目录、文本文件和mp3 文件)的情况。
我有以下shell脚本。
OUTPUT=$(systemctl is-active etcd)
if [[ $OUTPUT == active ]]; then
echo "The result is successfull"
else
echo "The result is unsuccessfull"
fi
Run Code Online (Sandbox Code Playgroud)
我想运行这个脚本 10 次,每次它会休眠 10 秒。我能够使用for i in {1..10}循环然后使用 sleep 命令来实现这一点。
for i in {1..10}; do
sleep 10
OUTPUT=$(systemctl is-active etcd)
if [[ $OUTPUT == active ]]; then
echo "The result is successfull"
else
echo "The result is unsuccessfull"
fi
done
Run Code Online (Sandbox Code Playgroud)
但是如果脚本在(例如第一次或第二次等)迭代期间与条件匹配并且不想执行下一次迭代,我想中断脚本。
我想我需要实现 while 循环,但我不确定如何在那里添加条件和 for 循环。
我有一个包含以下内容的文本文件
$ cat foo.txt
some text
email@id.com
8903457923
2018-02-09 07:12 (Asia/Kolkata)
again some text over here
some more text again
Message
some text
email@id.com
8903457923
2018-02-05 07:12 (Asia/Kolkata)
again some text over here
some more text again
Message
Run Code Online (Sandbox Code Playgroud)
我想获得以下输出
$ cat foo.txt
some text email@id.com 8903457923 2018-02-09 07:12 (Asia/Kolkata) again some text over her some more text again Message
some text email@id.com 8903457923 2018-02-05 07:12 (Asia/Kolkata) again some text over here some more text again Message
Run Code Online (Sandbox Code Playgroud)
我想我可以使用 tr 并将“消息”作为通用字符串来实现。但不确定如何实现这一点。
我有一个文本文件;其内容如下。
$ cat file.txt
[] [1]foo1 bar1
[] [2]foo2 bar2
[] [35]foo3 bar3
[] [445]foo4 bar4
[] [87898]foo5 bar5
Run Code Online (Sandbox Code Playgroud)
我可以使用 awk 成功删除第一列,但我无法删除 [num] 个字符,因为它与字符串关联。
我正在尝试获得如下输出
$ cat file.txt
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5
Run Code Online (Sandbox Code Playgroud) 我在文本文件中有以下文本
$ cat test
20180618:
20180619:
20180620:
20180621:
20180622:
20180623:
20180624:
Run Code Online (Sandbox Code Playgroud)
我试图用下面的数字范围进行 grep,
$ grep 201806{19..21} test
grep: 20180619: No such file or directory
grep: 20180620: No such file or directory
grep: 20180621: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我在 ZSH 和 bash 上都遇到了上述错误。似乎 grep 将搜索字符串作为文件。
我试过另一种方式:
$ grep 201806* test
zsh: no matches found: 201806*
Run Code Online (Sandbox Code Playgroud)
我只在 ZSH 上收到该错误。*在 ZSH 中使用的正确方法是什么,如何告诉 grep 到 grep 的数字范围?
我有一个以下脚本,它将从用户那里获取输入(源路径),并将在 docker 容器内附加卷
echo -n "Enter the source path: "
read path
docker run -v $path:/opt/$path/ fedora
Run Code Online (Sandbox Code Playgroud)
问题是,我想创建一个循环,以便用户可以提供多个源路径,并且可以将其附加到 docker 容器。
例如
docker run -v $path1:/opt/$path1 -v $path2:/opt/$path2
Run Code Online (Sandbox Code Playgroud)
等等,这些$path变量的数量取决于用户输入。
如果以下文件在所有行中包含相同的内容,则应打印“内容相同”。
$ cat file
example-line
example-line
example-line
Run Code Online (Sandbox Code Playgroud)
如果任何一行与其他行不同,那么它应该打印“内容不同”
例如
$ cat file
example-line
somethingelse
example-line
Run Code Online (Sandbox Code Playgroud)
将打印“内容不同”。
我在文件中有以下内容
foobar bar
bar foo foo
bar bar foobar
Run Code Online (Sandbox Code Playgroud)
我想从所有行中获取最后一个字符,也是最后 3 个字符。
我正在使用以下 for 循环解决方法来获得所需的输出,但我想cutcommand 可以做得比这好得多。有没有办法通过剪切来做同样的事情?
$ cat test.sh
FILE=$1
echo "Last chars:"
for i in $(cat $FILE)
do
echo ${i: -1}
# OR echo $i |tail -c 2
done
echo
echo "Last 3 chars:"
for i in $(cat $FILE)
do
echo ${i: -3}
# OR echo $i |tail -c 4
done
$ sh test.sh
Last chars:
r
r
r
o
o
r
r
r
Last 3 chars: …Run Code Online (Sandbox Code Playgroud)