简体   繁体   中英

Linux rm command with variable

My partners and I are having troubles with this basic Linux script. Our instructor dumped an advanced Linux scripting packet on us to debug and we are all pretty lost. The script is suppose to remove files containing embedded spaces in the file name and I am stuck on the actual rm command. 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 " " into the badname variable

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. Try this:

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

ls | grep " " > $TMPFILE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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