在我的/opt/myapp
目录中,我有一个远程自动流程,该流程将删除<anything>-<version>.zip
格式的文件,其中<anything>
可以任何字母数字filename,以及<version>
将是版本号的位置。因此,此自动化流程将提供的示例包括:
fizz-0.1.0.zip
buzz-1.12.35.zip
foo-1.0.0.zip
bar-3.0.9.RC.zip
等。通过此问题范围之外的控制,我保证在任何给定时间/opt/myapp
下只有其中一个ZIP文件存在。我需要编写一个 Bash shell命令,它将重命名这些文件并将它们移动到/opt/staging
。对于重命名,ZIP文件需要删除其版本。因此/opt/myapp/<anything>-<version>.zip
已重命名并移至/opt/staging/<anything>.zip
。使用上面的例子:
/opt/myapp/fizz-0.1.0.zip
=&gt; /opt/staging/fizz.zip
/opt/myapp/buzz-1.12.35.zip
=&gt; /opt/staging/buzz.zip
/opt/myapp/foo-1.0.0.zip
=&gt; /opt/staging/foo.zip
/opt/myapp/bar-3.0.9.RC.zip
=&gt; /opt/staging/bar.zip
目录移动显而易见,但重命名让我脱掉了头发。我需要以某种方式保存<anything>
,然后在命令中重新访问它。 该命令必须是通用的,不能参数。
到目前为止,我最好的尝试(甚至没有接近工作)是:
。文件= *拉链;文件= ?; mv文件/ opt / staging
关于如何做到这一点的任何想法?
答案 0 :(得分:2)
for file in *.zip; do
[[ -e $file ]] || continue # handle zero-match case without nullglob
mv -- "$file" /opt/staging/"${file%-*}.zip"
done
${file%-*}
会删除文件名中最后一个-
之后的所有内容。因此,我们将fizz-0.1.0.zip
更改为fizz
,然后添加前导/opt/staging/
和尾随.zip
。
为了使这更通用(使用多个扩展),请参阅以下函数(可以作为命令调用;函数体也可以放入带有#!/bin/bash
shebang的脚本中,如果删除了{{1声明):
local
...定义了该函数后,您可以运行:
stage() {
local file ext
for file; do
[[ -e $file ]] || continue
[[ $file = *-*.* ]] || {
printf 'ERROR: Filename %q does not contain a dash and a dot\n' "$file" >&2
continue
}
ext=${file##*.}
mv -- "$file" /opt/staging/"${file%-*}.$ext"
done
}
...或您选择的任何其他模式。
答案 1 :(得分:0)
while True:
question=input("do you like maths?")
re_do=input("Would you like to check anything else?")
if re_do.lower() =="yes":
continue
elif re_do.lower() =="no":
print "Goodbye!"
break
elif re_do.lower() !="no" and re_do.lower() !="yes":
x = input("Yes or No answer only: ")
if x == "yes":
continue
else:
print "Goodbye"
break