Script - issue with SED command

Hi folks,

I’m attaching an excerpt from a script I’m working at:

for i in {1..$line_nr_log}; do
    tb=$(sed "$i"'q;d '"$log"); #line i of file log.txt (cotnaining the file/dir to be backed up) is read into variable $tb
    cp -v --parents $tb $backupdir >> $temp_operations_log; #file $tb is copied to the backup directory. $tb contains the path of the file relative to /home. All directories contained in the relative path are copied in the backup directory in order to preserve the original path; output of the command obtained in verbose mode is redirected to the temp_operations_log file
done

I’m getting following errors when running bash -xv for this script:


for i in {1..$line_nr_log}; do
    tb=$(sed "$i"'q;d '"$log"); #line i of file log.txt (cotnaining the file/dir to be backed up) is read into variable $tb
    cp -v --parents $tb $backupdir >> $temp_operations_log; #file $tb is copied to the backup directory. $tb contains the path of the file relative to /home. All directories contained in the relative path are copied in the backup directory in order to preserve the original path; output of the command obtained in verbose mode is redirected to the temp_operations_log file
done
+ for i in '{1..$line_nr_log}'
sed "$i"'q;d '"$log")
sed "$i"'q;d '"$log"
++ sed '{1..2609}q;d /var/run/media/BACKUP/BACKUP/fbackup_03.05.2014/log'
sed: -e expression #1, char 3: unknown command: `.'
+ tb=
+ cp -v --parents
cp: missing file operand
Try 'cp --help' for more information.

I guess there is something wrong about the syntax of the SED command, however I can’t figure out which the issue is.

I’m getting the same errors when replacing the $(sed …) with sed ....

On the other hand when using sed without variables and only with single quotes I’m not getting any errors. Here’s an example:

central_backup_dir=`sed '1q;d' /home/test/bin/Backup_Scripts_Parameters`; #reading the path of the central backup directory, which is contained by the first line of the Backup_Scripts_Parameters file (this file contains certain parameters of the scripts fbackup.sh and dbackup.sh)

Thanks in advance for your help!

Cheers,
Liviu

As you can see, the $i is evaluated as {1…2609}, which I think is not what you want (and which sed (not SED btw) does not understand.

Thus IMHO you must concentrate on the

for i in {1..$line_nr_log}

to check what it does and what it should do.

brace expansions comes first before variable expansions. In this case it is seen as a literal $line_nr_log

for i in {1..$line_nr_log}

Use a C-style for loop instead.

for ((i=0;i<${#line_nr_log};i++))

Also “Don’t Read Lines With For”, use a while loop instead

while read -r line; do echo "$line"; done < log.txt

You might not need sed for this imo. You can paste the sample input of your file and a sample output that you desire. So the folks can see what are you really trying to do.

And please do not use the HTML tags, but the CODE tags. It is the # button.

If i understand correctly you are trying to loop over the files in a directory and run sed on each file using the for loop. If so you can use a glob instead

for i in ./*; do echo "${i#*/}"; done

If you need to do some test insert your test afer the do

for i in ./*; do  -f $i ]] && ....; done

This sed ‘1q;d’ reads the very first line of the file (if im not mistaken), if so you can test try this.

for i in ./*;do  -f "$i" ]] && read -r foo < "$i" && echo "$foo"; done

A lot of this is just a guess and it would end up in a guessing games unless you state what are you really trying to do aka EndGoal :wink:

If you are inside the directory of your files e.g. you have cd into it, try this and hopefully it will give you and idea.

for i in ./; do -f “$i” ]] && read -r FirstLine < “$i” && echo "First line of ${i##/} is $FirstLine"; done