简体   繁体   English

带有变量的Linux rm命令

[英]Linux rm command with variable

My partners and I are having troubles with this basic Linux script. 我和我的合作伙伴在使用此基本Linux脚本时遇到麻烦。 Our instructor dumped an advanced Linux scripting packet on us to debug and we are all pretty lost. 我们的讲师将一个高级Linux脚本包丢给我们进行调试,我们都迷失了。 The script is suppose to remove files containing embedded spaces in the file name and I am stuck on the actual rm command. 该脚本假定是要删除文件名中包含嵌入式空格的文件,而我却停留在实际的rm命令上。 I get "Missing Operand" and I am not sure what to do. 我得到“缺少操作数”,我不确定该怎么办。

#!/bin/bash

# This script is supposed to delete all filenames in current directory
#+ containing embedded spaces.
# It doesn't work
# Why not?


badname= ls | grep " "

# Try this:
echo "$badname"

rm "$badname"

exit 0

You need command substition to get the value of ls | grep " " 您需要命令替换来获取ls | grep " "的值。 ls | grep " " into the badname variable ls | grep " "进入badname变量

Either of the following options will work: 以下任一选项均可使用:

badname=`ls | grep " "`

or 要么

badname=$(ls | grep " ")

You can read more about command substitution here . 您可以在此处阅读有关命令替换的更多信息。 Hope this helps 希望这可以帮助

badname= ls | grep " " badname= ls | grep " " is obviously wrong. badname= ls | grep " "显然是错误的。 Try this: 尝试这个:

#!/bin/bash
TMPFILE="tmp.txt"

ls | grep " " > $TMPFILE

cat $TMPFILE | while read -r LINE
do
        rm "$LINE"
done

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

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