The grub2 shell aims to be a minimalistic bash like shell.
But how can I increment a variable in grub2?
In bash I would do:
var=$((var+1))
or
((var=var+1))
In grub2 I get a syntax error on these calls. How can I achieve this in the grub2 shell?
Grub2 does not have builtin arithmetic support. You need to add Lua support if you want that, see this answer for details.
Based on this answer (as already linked by other answer), the following appears to work with GRUB's regexp command (allows incrementing from any number 0-5, add more <from>,<to> pairs as needed):
num=0
incr="" ; for x in 0,1 1,2 2,3 3,4 4,5 5,6 ; do
regexp --set=1:incr "${num},([0-9]+)" "${x}"
if [ "$incr" != "" ] ; then
echo "$num incremented to $incr"
num=$incr
break
fi
done
Decrementing similarly works (just flipping two the regular expression parts):
num=6
decr="" ; for x in 0,1 1,2 2,3 3,4 4,5 5,6 ; do
regexp --set=1:decr "([0-9]+),${num}" "${x}"
if [ "$decr" != "" ] ; then
echo "$num decremented to $decr"
num=$decr
break
fi
done
Related
Taking count from file, say if count = 5, I want to print 5 variables. i.e. A B C D E.
If count = 2, Print 2 variables A B, etc.
I have tried using the ASCII values but couldn't go through it.
for i in {1..5}; do
count=5; a=0;
printf "\x$(printf %x '65+$a')";
count=count+1;
done
if count = 5, I want to print 5 variables. i.e. A B C D E. If count = 2, Print 2 variables A B, etc.
Here's a program that matches your style that does what you are looking for:
a=0
for i in {1..5}; do
printf "\x$(printf %x $(( 65 + a )) )";
a=$((a+1));
done
The first thing to note is that in order to do math in bash, you'll need to use the $(( )) operation. Above, you can see I replaced you '65+$a' with $(( 65 + a )) . That's the big news that you need to get math done.
There were a couple of other little issues, but you were stuck on the $(()) stuff so they weren't clear yet. Incidentally, the 'a' variable can be completely removed from the program to just use the 'i' variable like this:
for i in {1..5}; do
printf "\x$(printf %x $(( 64 + i )) )";
done
I had to change the constant to 64 since we are now counting starting at 1.
The {1..5} expression is a good short cut for 1 2 3 4 5, but you won't be able to put a variable into it. So, if you need to add a count variable back in, consider using the seq program instead like this:
count=$1
for i in $(seq 1 $count); do
printf "\x$(printf %x $(( 64 + i )) )";
done
Note that $() is different than the math operator $(()). $() runs a subcommand returning the results.
method 1: simple brace expansion
#!/bin/bash
# generate a lookup table
vars=( - $(echo {A..Z}) )
# use the elements
for i in {1..5}; do
echo ${vars[$i]}
done
{A..Z} generates 26 strings: A, B, ..., Z
which get stored in an array variable by vars=(...)
we prepend a - that we'll ignore
we can then do 1-based indexing into the array
limited to 26 variables (or whatever range we choose)
method 2: multiple brace expansion to generate arbitrary long variables
#!/bin/bash
if [[ ! $1 =~ ^[0-9]+$ ]]; then
echo "Usage: $0 count"
exit
fi
cmd='{A..Z}'
for (( i=$1; i>26; i=i/26 )); do
cmd="${A..Z}$cmd"
done
vars=( $(eval echo $cmd) )
for (( i=0; i<$1; i++ )); do
echo ${vars[$i]}
done
i/26 does integer division (throws away the remainder)
I'm lazy and generate "more than enough" variables rather than attempting to calculate how many is "exactly enough"
{a..b}{a..b}{a..b} becomes aaa aab aba abb baa bab bba bbb
using eval lets us do the brace expansion without knowing in advance how many sets are needed
Sample output:
$ mkvar.sh 10000 |fmt -64 | tail -5
ORY ORZ OSA OSB OSC OSD OSE OSF OSG OSH OSI OSJ OSK OSL OSM
OSN OSO OSP OSQ OSR OSS OST OSU OSV OSW OSX OSY OSZ OTA OTB
OTC OTD OTE OTF OTG OTH OTI OTJ OTK OTL OTM OTN OTO OTP OTQ
OTR OTS OTT OTU OTV OTW OTX OTY OTZ OUA OUB OUC OUD OUE OUF
OUG OUH OUI OUJ OUK OUL OUM OUN OUO OUP
In a for loop I am looking to find out: If Array1 length matches Array2 length then break the for loop.
Shellcheck throws an error (while the script runs fine)
if [[ "${!Array1[#]}" == "${!Array2[#]}" ]] ; then
break;
fi
^-- SC2199: Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of #).
I'm still learning bash and my teacher said "Always verify with Shellcheck" and to "always place conditionals in double [[ ]]" and to "never use * for array length"
The error(s) are removed with the correction
if [ "${!Array1[*]}" == "${!Array2[*]}" ] ; then
break;
fi
I was wondering what is the best practice here?
Your code is partly correct. The problem is, that you are expanding the array's indicies with th ! operator, and not the length by using the # operator.
Thus a warning about implicit concatenation is issued for both uses of ${array[#]}, as the list of indicies is 0 1 2 3 .... Nevertheless your code is working, as two non associative bash arrays with equal length will have identical index lists 0 1 2 .. N.
To get rid of the warning, you should replace ${!array[#]} with ${#array[#]}. Of course, using ${!array[*]} will also suppress the warning, but that is definitely not what you want to do here, as you would continue comparing index lists.
For further reading: The author of shellcheck has explained the concatenation issue here in detail.
Nyronium's works well and explains the solution perfectly.
I also found a solution that utilities # to check the array length for the condition.
Sorry I did not have this example script before, as I wrote it to test logic afterwards.
Example
#!/bin/bash
array1=(1 2 3)
array2=()
echo "array1 length: ${#array1[#]}" #output :3
for i in {1..10} ; do
echo "array2 length: ${#array2[#]}"
array2+=("$i")
#I want to exit when array2 = a length of 3
if [[ "${#array1[#]}" == "${#array2[#]}" ]] ; then
echo "exit"
break
fi
done
echo "final array1 length: ${#array1[#]} vs array2 length: ${#array1[#]}"
Result: final array1 length: 3 vs array2 length: 3
I'm working with an existing script which was written a bit messily. Setting up a loop with all of the spaghetti code could make a bigger headache than I want to deal with in the near term. Maybe when I have more time I can clean it up but for now, I'm just looking for a simple fix.
The script deals with virtual disks on a xen server. It reads multipath output and asks if particular LUNs should be formatted in any way based on specific criteria. However, rather than taking that disk path and inserting it, already formatted, into a configuration file, it simply presents every line in the format
'phy:/dev/mapper/UUID,xvd?,w',
UUID, of course, is an actual UUID.
The script actually presents each of the found LUNs in this format expecting the user to copy and paste them into the config file replacing each ? with a letter in sequence. This is tedious at best.
There are several ways to increment a number in bash. Among others:
var=$((var+1))
((var+=1))
((var++))
Is there a way to do the same with characters which doesn't involve looping over the entire alphabet such that I could easily "increment" the disk assignment from xvda to xvdb, etc?
To do an "increment" on a letter, define the function:
incr() { LC_CTYPE=C printf "\\$(printf '%03o' "$(($(printf '%d' "'$1")+1))")"; }
Now, observe:
$ echo $(incr a)
b
$ echo $(incr b)
c
$ echo $(incr c)
d
Because, this increments up through ASCII, incr z becomes {.
How it works
The first step is to convert a letter to its ASCII numeric value. For example, a is 97:
$ printf '%d' "'a"
97
The next step is to increment that:
$ echo "$((97+1))"
98
Or:
$ echo "$(($(printf '%d' "'a")+1))"
98
The last step is convert the new incremented number back to a letter:
$ LC_CTYPE=C printf "\\$(printf '%03o' "98")"
b
Or:
$ LC_CTYPE=C printf "\\$(printf '%03o' "$(($(printf '%d' "'a")+1))")"
b
Alternative
With bash, we can define an associative array to hold the next character:
$ declare -A Incr; last=a; for next in {b..z}; do Incr[$last]=$next; last=$next; done; Incr[z]=a
Or, if you prefer code spread out over multiple lines:
declare -A Incr
last=a
for next in {b..z}
do
Incr[$last]=$next
last=$next
done
Incr[z]=a
With this array, characters can be incremented via:
$ echo "${Incr[a]}"
b
$ echo "${Incr[b]}"
c
$ echo "${Incr[c]}"
d
In this version, the increment of z loops back to a:
$ echo "${Incr[z]}"
a
How about an array with entries A-Z assigned to indexes 1-26?
IFS=':' read -r -a alpharray <<< ":A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"
This has 1=A, 2=B, etc. If you want 0=A, 1=B, and so on, remove the first colon.
IFS=':' read -r -a alpharray <<< "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"
Then later, where you actually need the letter;
var=$((var+1))
'phy:/dev/mapper/UUID,xvd${alpharray[$var]},w',
The only problem is that if you end up running past 26 letters, you'll start getting blanks returned from the array.
Use a Bash 4 Range
You can use a Bash 4 feature that lets you specify a range within a sequence expression. For example:
for letter in {a..z}; do
echo "phy:/dev/mapper/UUID,xvd${letter},w"
done
See also Ranges in the Bash Wiki.
Here's a function that will return the next letter in the range a-z. An input of 'z' returns 'a'.
nextl(){
((num=(36#$(printf '%c' $1)-9) % 26+97));
printf '%b\n' '\x'$(printf "%x" $num);
}
It treats the first letter of the input as a base 36 integer, subtracts 9, and returns the character whose ordinal number is 'a' plus that value mod 26.
Use Jot
While the Bash range option uses built-ins, you can also use a utility like the BSD jot utility. This is available on macOS by default, but your mileage may vary on Linux systems. For example, you'll need to install athena-jot on Debian.
More Loops
One trick here is to pre-populate a Bash array and then use an index variable to grab your desired output from the array. For example:
letters=( "" $(jot -w %c 26 a) )
for idx in 1 26; do
echo ${letters[$idx]}
done
A Loop-Free Alternative
Note that you don't have to increment the counter in a loop. You can do it other ways, too. Consider the following, which will increment any letter passed to the function without having to prepopulate an array:
increment_var () {
local new_var=$(jot -nw %c 2 "$1" | tail -1)
if [[ "$new_var" == "{" ]]; then
echo "Error: You can't increment past 'z'" >&2
exit 1
fi
echo -n "$new_var"
}
var="c"
var=$(increment_var "$var")
echo "$var"
This is probably closer to what the OP wants, but it certainly seems more complex and less elegant than the original loop recommended elsewhere. However, your mileage may vary, and it's good to have options!
I am trying to read a file in bash forloop. But I do not know how to put write the script for this.
for i in $( seq 0 $step 10 )
do
echo "Rendering: "$(( i + j ))
python auto_fine.py density000000.vtu velocity000000.vtu $(( i + j ))
done
each and every loop I need to call
i -> 0 python auto_fine.py density000000.vtu velocity000000.vtu
i -> 1 python auto_fine.py density000010.vtu velocity000010.vtu
i -> 2 python auto_fine.py density000020.vtu velocity000020.vtu
It seems to me that you need to zero pad the numbers sed provides to you:
As seen in How to zero pad a sequence of integers in bash so that all have the same width?, you need to do something like
$ seq -f "%06g" 0 10 100
Which returns:
000000
000010
000020
...
000100
All together,
for i in $(seq -f "%06g" 0 10 100)
do
# echo "Rendering: "$(( i + j )) -- not sure what this does
python auto_fine.py density$i.vtu velocity$i.vtu
done
Bash can do this without requiring external tools like seq.
for i in {0..100}; do
[[ $i = *0 ]] || continue
python auto_fine.py density$(printf '%06d' $i).vtu velocity$(printf '%06d' $i).vtu
done
This uses pattern matching (*0) to limit your list to every 10 numbers, which is a bit of a hack, but will work against your sample data.
You could alternately loop against your zero-padded numeric strings directly:
for i in $(printf '%05d0 ' {0..10}); do
python auto_fine.py density$i.vtu velocity$i.vtu
done
This option shows you every 10 items by placing a zero in the printf format after the incrementing number, which becomes the tens digit. If you want more arbitrary sequencing, you might use multipliers, still without spawning external processes:
low=0
high=100
mult=10
for i in $(eval echo {$low..$((high/mult))}); do
n=$(printf '%06d' $((i*mult)))
python auto_fine.py density$n.vtu velocity$n.vtu
done
Note the eval, which lets you expand variables for use in your sequence expression. (If you are getting these numbers from an external source, have your script validate them before using them!)
If you're using bash version 4 (i.e. not the native version on OSX), you also have increments available in sequence expressions. From the man page:
A sequence expression takes the form {x..y[..incr]}, where x and y are
either integers or single characters, and incr, an optional increment,
is an integer.
So perhaps:
low=0
high=100
mult=10
for i in $(eval "printf '%06d ' {$low..$high..$mult}"); do
python auto_fine.py density$i.vtu velocity$i.vtu
done
Note that in sequence expressions, the first member of the sequence is the first number provided, rather than merely a product of a multiplier. We have quotes around the printf to ensure that the sequence expression is expanded by eval, and not interpreted by the command substitution ($(..)).
looping for all the files in the current dir is trivial:
for i in $( ls -1 )
do
# your code here, variable is referenced with $i
done
what's the j variable you are using?
I am trying to print a filename which contains decimal point numbers...
say
L2.3stop.txt
I have variables defined as :
z1=2.3
z2=3.4
z3=7.8
z4=8.9
and so on
In a for loop i runs from 1 to 5
Inside the loop if I do
temp=`echo z$i`
and then I print the file name using
echo L${temp}stop.txt
it just prints
Lz1stop.txt
Lz2stop.txt
etc..
How can I print the desired filename....
I also tried using
echo L$((z$i))stop.txt
but this only works when z1, z2, z3 etc are integers and not floating numbers....
I hope this helps:
z1=2.3
z2=3.4
z3=7.8
z4=8.9
for i in {1..4};do
echo L$(eval "echo \$z"$i)stop.txt
done
or this should work too:
for i in {1..4};do
echo L$(echo $(echo "\$z$i"))stop.txt
done
outputs:
L2.3stop.txt
L3.4stop.txt
L7.8stop.txt
L8.9stop.txt
This is not portable, but in bash you can do:
name=z$i
echo ${!name}
Best (using an array):
z=( 2.3 3.4 7.8 8.9 9.8 ) # Added fifth value
for x in "${z[#]}"
do
echo L${x}stop.txt
done
Less good (using indirect expansion — a bash feature):
z1=2.3
z2=3.4
z3=7.8
z4=8.9
z5=9.8 # Added fifth value
for i in {1..5}
do
v=z$i
echo L${!v}stop.txt
done
Both produce:
L2.3stop.txt
L3.4stop.txt
L7.8stop.txt
L8.9stop.txt
L9.8stop.txt
Both avoid using eval which is not too harmful in this context but is dangerous (and difficult to use correctly) in general.
If you have defined variable z, you can do this:
for v in ${!z*}; do echo L${!v}stop.txt; done
or:
for v in z{1..4}; do echo L${!v}stop.txt; done