BASH ERROR when trying to get data from zimbra: value too great for base - bash

Although this error is quite common and explained almost everywhere on the web I've decided to ask a new question since I can't get a clue for this specific case.
I'm trying to get some data out of Zimbra Collaboration Suite and the only way I can do it is via bash.
Being my first time with bash I find it a bit hard to deal with.This is the code:
#!/bin/bash
all_account=`zmprov -l gaa`;
declare -i szquota
szquota=524288000
for account in ${all_account}
do
mb_size=`zmmailbox -z -m ${account} gms`;
set -i size;
declare -i quota
declare -i quota2
for size in ${mb_size}
do
if [ $((10#$size)) -gt $((10#$szquota)) ] ; then
quota=`zmprov ga ${account} zimbraMailQuota`;
quota2="10#`zmprov ga ${account} zimbraMailQuota`";
echo "${account},${mb_size},$quota2\n";
fi
done
done
and this is the response:
line 12: 137,08: value too great for base (error token is "08")
I need to print all the accounts that have a quota of more than 500MB, and the output should be like this: account/quota/used quota.
Since mb_size is an array of values I can't figure out how I could convert its content to a decimal base as I did with the other values?
It probably is much simpler than my mind makes it look but I really can't get out of this trouble.
Kind regards
EDIT:
Thanks #Alfe!
I've modified the code like this:
#!/bin/bash
all_account=`zmprov -l gaa`;
szquota=524288000
for account in ${all_account}
do
mb_size=`zmmailbox -z -m ${account} gms`;
declare -i quota
declare -i quota2
for sizeStr in ${mb_size}
do
if [ $size -gt $((10#$szquota)) ] ; then # <--- line 13
quota=`zmprov ga ${account} zimbraMailQuota`;
quota2="10#`zmprov ga ${account} zimbraMailQuota`";
echo "${account},${mb_size},$quota2\n";
fi
done
done
but it returns another error:
line 13: [: -gt: unary operator expected
I've also tried to enclose the values inside the if clause between quotation marks but if I'm not wrong bash interprets the content of quotation marks as a string and gives back this:
line 13: [: : integer expression expected
I'm sure I'm getting closer to a solution but I'm still stuck at the moment.

You declared the variable size as integer with set -i size. Later you try to assign a value which then is checked for being a valid integer, it isn't, then you get the error. In your case, one of the values in ${mb_size} is the string 08 which is then interpreted as a bad octal value.
I suggest you let the loop run over a different variable which is not declared as int (so the for statement won't posed a problem), then as first statement you assign the string value of the loop variable properly so that it does not get interpreted as octal:
for sizeStr in ${mv_size}
do
size=$((10#$sizeStr))
if [ $size -gt $((10#$szquota)) ]
then
…
You could of course also just remove the declaration of the variable as integer. This probably would also solve the issue.

Related

I would like to automate data extraction from an API and a for-loop using a Bash script but getting an error

#!/bin/bash
declare -a gpu_array=("rtx3060", "rtx3070", "rtx3080", "rtx3090", "rx6700")
#host = "http://192.168.1.31:5000/"
for gpu in ${gpu_array[*]}; do
gpu_url = $"http://192.168.1.31:5000/$gpu"
value = $(curl "${gpu_url}")
echo $value>> trial.txt
done
"""
trial.sh: line 8: value: command not found
trial.sh: line 7: gpu_url: command not found
curl: (3) URL using bad/illegal format or missing URL
trial.sh: line 8: value: command not found
trial.sh: line 7: gpu_url: command not found
curl: (3) URL using bad/illegal format or missing URL
trial.sh: line 8: value: command not found
"""
The array elements are separated by spaces, no commas. The double quotes are not needed, as the elements don't contain any special characters, but they don't hurt.
declare -a gpu_array=(rtx3060 rtx3070 rtx3080 rtx3090 rx6700)
Don't use $"...", it's used for localization. Plain "..." is enough here, in fact, you don't need any quotes here:
gpu_url=http://192.168.1.31:5000/$gpu
Note there are no spaces around the assignment operator. If you use spaces, bash takes the variable name as the command to run and = as its first parameter. That's not what you usually want.
gpu_url=http://192.168.1.31:5000/$gpu
value=$(curl "$gpu_url")
BTW, the standard way of iterating over the array would be
for gpu in "${gpu_array[#]}"; do
It'd work correctly even if the array elements contained spaces.
For the same reason, it's a good practice to always double quote the variables (unless you want to split the value on whitespace).
echo "$value"
This can still do something unexpected if the value is -n in some shells, so it's safer to use echo only for constant strings and use printf for dynamic contents.
printf '%s\n' "$value"

Check if file does have different permission than 644 in Bash

I have to change permission of a file to 644 if the file does have a different permission than 644.
I have thought of something like this:
if [ $(stat -c %A $soubor) ! -eq (-rw-r--r--) ];then...
However, it gives errors like:
integer expression expected
or if I modify it a little, then:
syntax error):
You're using [ wrong. It's the same as test, so man test gives you the manual page to it.
Basically, [ is a program you call with arguments. For nicer looks, [ requires the last paramter to be ], but there is not technical reason for that.
When you call [ $a == $b ], it fails because [ will compare $a to = and complain about the $b which it does not expect. So if you want to develop it POSIX-compliant, you need to use = instead of ==.
-eq does a numeric compare so -rw-r--r-- will cause a syntax error because it's not numeric.
= will do a string compare, so this is what you need.
You compare with (-rw-r--r--) which will be evaulated as command, so you need to add quotes around it (see my comment on your question, I used the octal syntax).
Also, you need to remove the braces so it becomes '-rw-r--r--'. I assume you added them because they were on the left side of your if statement.
The reason for the braces on the left is command execution. When you wrap something into $(), it will get executed and the output of the command will be filled in there. So after the shell evaluated the expression, it can look like this:
'[' '-rw-rw-rw-' '=' '-rw-r--r--' ']'
When you use set -x anywhere in your shell script, you can see what it acutally does which helps a lot. You can disable it with set +x afterwards as it's very verbose.

How to count number of lines in file and then use maths on that variable in bash?

I am currently using a bash script to pipe a few other codes together but am new and have been stuck on this for the last day or so. Basically I need to count the number of lines within a file and then divide that by 4 to get the true number of objects in that file (each object takes up 4 lines).
For this I have looked around and ended up with the following code:
a=$(wc -l "${o}"*)
k=$(wc -l Unmatched_forward.fq)
x=4
#declare -i $a
declare -i $k
stats1_2=$((a / x))
stats2_2=$((k / x))
echo "${stats1_2} reads were joined."
echo "${stats2_2} reads were not joined."
Within this code ${o} is the output from a previous file however needs to have ".fq" added to the end but whenever I try to add that to the end it comes up the error message below I have been trying to use the "*" to run on the file of which there are no other files similar.
"Unmatched_forward.fq" is another output file which I want to count the number of objects in.
I am using the declare option because I read that otherwise the number will be in string form instead of an integer and so maths cannot be done.
If anyone can help and explain whats wrong that would be great.
The error message is:
Overlay_code.sh: line 638: declare: `1265272': not a valid identifier
Overlay_code.sh: line 638: declare: `Unmatched_forward.fq': not a valid identifier
Overlay_code.sh: line 643: 1265272 Unmatched_forward.fq: syntax error: invalid arithmetic operator (error token is ".fq")
Whats more confusing is I am suddenly getting the '1265272' number appearing and have no idea why!
You should check that your invocation of wc truly returns only an integer, because I think it is not. Probably the following happens
$> wc -l Unmatched_forward.fq
128 Unmatched_forward.fq
So it returns the line count and the filename.
The following should work
k=$(wc -l Unmatched_forward.fq | awk '{print $1}')
x=4
stats1_2=$((k / x))
Note that bash's (()) only supports integer math, so all results will get rounded. If you need floating point precision, check out bc
You mean declare -i k. When you include the $, you're causing the variable name to be replaced with its value. But you want to say that the variable k is integer-typed.

overcome sqlplus substitution variable value length limitation shell script

I have a requirement to pass >1000 char length argument to sql*plus.
When checked the docs Sql*Plus has a substitution variable value length limitation of 240 chars.It get the following error when i try
string beginning "XXXXXX,..." is too long. maximum size is 239 characters.
Is there any ways to overcome this..??
I am thinking of splitting the input into parts and calling Sql Plus multiple times in loop.
Is this the only way or any other optimal solution is present..??
Here is my code
if [ ! "" == "$ship" ];
then
sqlplus -S ${DBUSER}/${DBPASSWD}#${DBWRITE} #close_tpdoc_task.sql $res $comm $ship $id
fi
Kindly advice

comparing variables in bash script using if statements

So I'm pretty new to bash scripting but so far tldp.org has been a good friend. Anyways I've confused myself and swearing to much so looking for help in clarification: I declare a variable like such
MAXseeds=-1;
sumS=0
I do a bunch of things in my script and get a new value for sumS which is an integer value. I would then like to compare MAXseeds and sumS if sumS is larger make MAXseeds equal to sumS. I do this by:
echo $MAXseeds
echo $sumS
if [ $MAXseeds -lt $sumS ];
then
MAXseeds = $sumS
best_file=$COUNT
fi
echo $MAXseeds
This from what I can tell should work however the terminal output I get when running over this section of script is
-1
492
lookup.sh: line 34: MAXseeds: command not found
-1
Basically I am wondering what I am doing wrong here? why does it respond with command not found? Any explanation to why this is incorrect would be greatly appreciated.
Try this:
if [ $MAXseeds -lt $sumS ];
then
MAXseeds=$sumS
best_file=$COUNT
fi
Without the spaces around "=".
If you put a space after "MAXseeds", then it will be interpreted as a command. And of course, it is not a command, thus you get your error message.

Resources