My bash scripts has a variable $c
which is called within a sed
-line, directly followed by another parameter - and bash (actually fairly logical) seems to think that the other parameter belong to the variable name, rendering it useless/empty.
(...)
c=$(printf "%02d" $(echo "$i+1"|bc))
sed -n "$cq;d" /var/www/playlisten.txt|cut -c 4-
(...)
The first line sets a temp variable, then it is called as a sed argument. I need to show bash that $c
ends after the c and that the variable is not named $cq
(which is empty, of course)...
Any ideas would be highly appreciated, as always.
Thanks, Christian.
PS. What I'm trying to accomplish :) with this is having a for-loop that steps thru 00..50, within the loop the number itself is needed but also the number +1. Just in case anyone want's to know.
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.
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
The shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts. This system shell can vary from OS to OS(most of the time it will be bash).
You need to use ${c}q
to prevent the greedy treatment (bash
trying to use as many valid characters as possible):
pax$ export cq=111
pax$ export c=222
pax$ echo $cq
111
pax$ echo ${c}q
222q
I should also mention that, if performance is important to you, you want to try and minimise how many external processes (like bc
) you run to do your task. Forking and exec'ing are not cost-free actions and you'll run much faster if you get bash
to do most of the work itself for short lived tasks.
By short-lived I mean things like adding one to a variable. Obviously if you want to do a big job like sed
an entire file, you're better off doing that in a dedicated external tool but bash
provides quite a bit of power to replace expensive operations like i=$(expr $i + 1)
or i=$(echo "$i+1"|bc)
.
A bash
loop can be done thus with the increment and other calculations being handled without external processes:
#!/bin/bash
for ((count = 0; count < 10; count++)) ; do
((next = count + 1))
echo "count is ${count}, sed string is '${next}q;d'."
done
This outputs:
count is 0, sed string is '1q;d'.
count is 1, sed string is '2q;d'.
count is 2, sed string is '3q;d'.
count is 3, sed string is '4q;d'.
count is 4, sed string is '5q;d'.
count is 5, sed string is '6q;d'.
count is 6, sed string is '7q;d'.
count is 7, sed string is '8q;d'.
count is 8, sed string is '9q;d'.
count is 9, sed string is '10q;d'.
You could also incorporate next
into the for
loop as well:
#!/bin/bash
for ((count = 0, next = 1; count < 10; count++, next++)) ; do
echo "count is ${count}, sed string is '${next}q;d'."
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With