Basename gives extra operand error

14,859

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.

Share:
14,859

Related videos on Youtube

eric moon
Author by

eric moon

Systems Engineer

Updated on September 18, 2022

Comments

  • eric moon
    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
      Abdul over 10 years
      Shouldn't it be awk '{print $9}'?
    • eric moon
      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
      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'
    Gilles 'SO- stop being evil' over 10 years
    Note that FILES=($(command)) is only fine when the file names don't contain whitespace or \[?*.