Basename gives extra operand error
FILES=$(command)
is going to set a variable named FILES
to a scalar that contains the output of command
.
${FILES[0]}
is going to be the contents of that scalar variable, in your case, a string containing 15 blank-delimited filenames, which will then be broken up into 15 fields.
It looks like you want FILES
to be an array; for that, use FILES=( $(command) )
.
It's also good practice to surround shell variable accesses with double quotes - "${FILES[0]}"
instead of ${FILES[0]}
- so that the shell doesn't break the contents into fields if there's any whitespace in them.
Related videos on Youtube
Comments
-
eric moon over 1 year
I 'm using the following script to merge few pcap files into one using mergecap command. But when I run it it gives me a
'basename: extra operand /mnt/md0/capture/DCN/dcn_2014_02_04_00_11_47_598.pcap'
error The script is as follows#!/bin/bash #find last 15 files older than +5 days FILES=$(find /mnt/md0/capture/DCN/ -maxdepth 1 -type f -name "*.pcap" -mtime +5 -print0 | xargs -0 ls -lt | tail -15 | awk '{print $8}') N=15 TAG1=$(basename ${FILES[0]} | sed 's/.pcap//') TAG2=$(basename ${FILES[$N-1]} | sed 's/.pcap//') #merge the files mergecap -w /mnt/md0/capture/DCN/"${TAG1}_to_${TAG2}".pcap ${FILES[@]} sudo chmod +rw /mnt/md0/capture/DCN/"${TAG1}_to_${TAG2}".pcap #delete originals sudo rm ${FILES[@]}
-
Abdul over 10 yearsShouldn't it be
awk '{print $9}'
? -
eric moon over 10 years@Abdul yes I thought so, but it seems like in this system $8 is giving me the file names. I tested it manually to be sure
-
Stéphane Chazelas over 10 years
tag1=$(basename "${FILES[0]}" .pcap
. Remember that leaving a variable unquoted is the split+glob operator in shells.
-
-
Gilles 'SO- stop being evil' over 10 yearsNote that
FILES=($(command))
is only fine when the file names don't contain whitespace or\[?*
.