Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation in Bash

Tags:

string

bash

My code

#!/bin/bash
for (( c=0; c<=1127; c++ ))
do
id = 9694 + c
if (id < 10000); then
    wget http://myurl.de/source/image/08_05_27_0${id}.jpg
else 
    wget http://myurl.de/source/image/08_05_27_${id}.jpg
fi
done 

I only get

./get.sh: line 5: 10000: No such file or directory
--2009-05-06 11:20:36--  http://myurl.de/source/image/08_05_27_.jpg

without the number.

The corrected code:

#!/bin/bash
for (( c=0; c<=1127; c++ ))
do
id=$((9694+c))
if (id -lt 10000); then
    wget http://myurl.de/source/image/08_05_27_0${id}.jpg
else 
    wget http://myurl.de/source/image/08_05_27_${id}.jpg
fi
done 

And even better:

for i in $(seq 9694 10821) ; do
    _U=`printf "http://myurl.de/source/image/08_05_27_%05d.jpg" $i`
    wget $_U 
done
like image 375
ByteNirvana Avatar asked May 06 '09 09:05

ByteNirvana


People also ask

What is interpolation Linux?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do you increment a variable in bash?

Increment Bash Variable with += Operator Another common operator which can be used to increment a bash variable is the += operator. This operator is a short form for the sum operator. The first operand and the result variable name are the same and assigned with a single statement.

How do you set variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.


2 Answers

I'll opt for simpler solution

for i in $(seq 9694 10821) ; do
    _U=`printf "http://myurl.de/source/image/08_05_27_%05d.jpg" $i`
    wget $_U 
done
like image 105
greg Avatar answered Sep 18 '22 15:09

greg


You are making a couple of mistakes with bash syntax, especially when dealing with arithmetic expressions.

  • You cannot put a space around the = sign when assigning to a variable.
  • In the assignment to "id", to invoke arithmetic evaluation, you need to use the $(( expression )) syntax.
  • For the "if" condition, you need double parentheses just like you're using with "for".

This should work:

#!/bin/bash
for (( c=0; c<=1127; c++ )); do
  id=$((9694 + c))
  if ((id < 10000)); then
    wget http://myurl.de/source/image/08_05_27_0${id}.jpg
  else
    wget http://myurl.de/source/image/08_05_27_${id}.jpg
  fi
done
like image 32
Ville Laurikari Avatar answered Sep 19 '22 15:09

Ville Laurikari