I have a problem with a simple bash script..
#!/bin/bash
libc_main_ver=$(dpkg -l |grep libc6 |awk '{print$3}' |awk -F .
'{print$1}'|xargs -n 1)
if [ "$libc_main_ver" -eq 2 ] ;then
echo ok
else
echo nope
fi
exit 0
When i try execute the script it gives me the error:
2: integer expression expected
What's the problem?
dpkg -l |grep libc6 |awk '{print$3}' |awk -F . '{print$1}'|xargs -n 1
This prints only 2 not other values.
Some suggestions?
Insert
echo "<$libc_main_ver>"
after the first line and the error will be obvious. Very likely there's a space or newline in there somewhere which you shouldn't numerically compare with 2.
Very esoteric: the problem could be the literal in -eq 2 as well. If it is followed by a carriage return character, I can somewhat reproduce your error message:
$ test "2" -eq 2^R
bash: test: 2: integer expression expected
where ^R is a literal carriage return entered with CTRL-V CTRL-R. To verify or exclude this, run od -bc nameofyourscript
What if you tried this:
#!/bin/bash
libc_main_vers=$(dpkg -l | grep libc6 | awk '{ print $3 }' | awk -F . '{ print $1 }')
for ver in ${libc_main_vers}; do
if (( ver == 2 )); then
echo ok
else
echo nope
fi
done
exit 0
Here is another version:
#!/bin/bash
libc_full=$(dpkg -l | grep libc6 | head -n 1 | awk '{ print $3 }')
libc_main=$(echo ${libc_full} | grep -o "^[0-9]*")
libc_update=$(echo ${libc_full} | grep -o "[0-9]*$")
if (( libc_main == 2 )); then
echo "Main version is OK"
if (( libc_update >= 7 )); then
echo "Update version is OK"
exit 0
fi
fi
exit 1
It's unclear whether you require all the packages you list to have a major version number 2, or are just verifying that at least one of them does.
Anyway, if we start with the observation that grep foo | awk { print $1 } can usually be refactored into a single Awk script awk '/foo/ { print $1 }' we can already simplify your script; but it sometimes makes sense to refactor basically all of it into Awk. Perhaps like this:
dpkg -l 'libc6*' | awk '$3 !~ /^2\./ { exit 1 }' && echo ok || echo nope
If you are satisfied with just one package meeting the condition, change to something like
dpkg -l 'libc6*' | awk '$3 ~ /^2\./ { exit 0 } END { exit 1 }' && echo ok || echo nope
As always, foo && success || failure is a shorthand for
if foo; then
success
else
failure
fi
where the longhand probably makes sense if your real script needs to do something moderately more complex than just echo a value.
Do note that the output from dpkg -l is not necessarily suitable for scripts. Maybe use dpkg-query -f '${Version}\n' -W 'libc6' instead for robustness.
Related
I have directory containing files:
$> ls blender/output/celebAnim/
0100.png 0107.png 0114.png 0121.png 0128.png 0135.png 0142.png 0149.png 0156.png 0163.png 0170.png 0177.png 0184.png 0191.png 0198.png 0205.png 0212.png 0219.png 0226.png 0233.png 0240.png 0247.png 0254.png 0261.png 0268.png 0275.png 0282.png
0101.png 0108.png 0115.png 0122.png 0129.png 0136.png 0143.png 0150.png 0157.png 0164.png 0171.png 0178.png 0185.png 0192.png 0199.png 0206.png 0213.png 0220.png 0227.png 0234.png 0241.png 0248.png 0255.png 0262.png 0269.png 0276.png 0283.png
0102.png 0109.png 0116.png 0123.png 0130.png 0137.png 0144.png 0151.png 0158.png 0165.png 0172.png 0179.png 0186.png 0193.png 0200.png 0207.png 0214.png 0221.png 0228.png 0235.png 0242.png 0249.png 0256.png 0263.png 0270.png 0277.png 0284.png
0103.png 0110.png 0117.png 0124.png 0131.png 0138.png 0145.png 0152.png 0159.png 0166.png 0173.png 0180.png 0187.png 0194.png 0201.png 0208.png 0215.png 0222.png 0229.png 0236.png 0243.png 0250.png 0257.png 0264.png 0271.png 0278.png
0104.png 0111.png 0118.png 0125.png 0132.png 0139.png 0146.png 0153.png 0160.png 0167.png 0174.png 0181.png 0188.png 0195.png 0202.png 0209.png 0216.png 0223.png 0230.png 0237.png 0244.png 0251.png 0258.png 0265.png 0272.png 0279.png
0105.png 0112.png 0119.png 0126.png 0133.png 0140.png 0147.png 0154.png 0161.png 0168.png 0175.png 0182.png 0189.png 0196.png 0203.png 0210.png 0217.png 0224.png 0231.png 0238.png 0245.png 0252.png 0259.png 0266.png 0273.png 0280.png
0106.png 0113.png 0120.png 0127.png 0134.png 0141.png 0148.png 0155.png 0162.png 0169.png 0176.png 0183.png 0190.png 0197.png 0204.png 0211.png 0218.png 0225.png 0232.png 0239.png 0246.png 0253.png 0260.png 0267.png 0274.png 0281.png
For some script, I will need to find out what the number of the first missing file is. In the above output, it would be 0285.png. However, it is also possible that files in between are missing. In the end, I am only interested in the number 285, which is part of the file name.
This is part of recovery logic: The files should be created by the script, but this step can fail. Therefore I want to have a means to check which files are missing and try to create them in a second step.
This is what I got so far (from how to extract part of a filename before '.' or before extension):
ls blender/output/celebAnim/ | awk -F'[.]' '{print $1}'
What I cannot figure out is how do I find the smallest number missing from that result, above a certain offset? The offset in this case is 100.
You could loop over all number from 100 to 500 and check if the corresponding file exists; if it doesn't, you'd print the number you're looking at:
for i in {100..500}; do
[[ ! -f 0$i.png ]] && { echo "$i missing!"; break; }
done
This prints, for your example, 285 missing!.
This solution could be made a bit more flexible by, for example, looping over zero padded numbers and then extracting the unpadded number:
for i in {0100..0500}; do
[[ ! -f $i.png ]] && { echo "${i##*(0)} missing!"; break; }
done
This requires extended globs (shopt -s extglob) for the *(0) pattern ("zero or more repetitions of 0").
begin=100
end=500
for i in `seq $begin 1 $end`; do
fname="0"$i".png"
if [ ! -f $fname ]; then
echo "$fname is missing"
fi
done
#!/bin/sh
search_dir=blender/output/celebAnim/
ls $search_dir > file_list
count=`wc -l file_list | awk '{ print $1 }'`
if [[ $count -eq 0 ]]
then
echo "No files in given directory!"
break
fi
file_extension=`head -1 file_list | tail -1 | awk -F "." '{ print $2 }'`
init_file_value=`head -1 file_list | tail -1 | awk -F "." '{ print $1 }'`
i=2
while [ $i -le $count ]
do
next_file_value=`head -$i file_list | tail -1 | awk -F "." '{ print $1 }'`
next_value=$((init_file_value+1));
if [ $next_file_value -ne $next_value ]
then
echo $next_value"."$file_extension
break
fi
init_file_value=$next_value;
i=$((i+1));
done
try it:
ls blender/output/celebAnim/ | sort -r | head -n1 | awk -F'.' '{print $1+1}'
command return 285
if need return 0285 than try it:
ls blender/output/celebAnim/ | sort -r | head -n1 | awk -F'.' '{print 0($1+1)}'
I don't know why I get an extra 0 when I run my script.
This is my script: I run a SQL query and save it ta an file valor.txt.
This is my array: array=(50 60 70)
Valor.txt:
count | trn_hst_id | trn_msg_host
-------+------------+--------------
11 | 50 | Aprobada
2 | 70 | Aprobada
(2 rows)
Code:
function service_status {
cd
cat valor.txt | grep $1 | gawk '{print $1}' FS="|" | sed "s/ //g"
if [ $? -eq 0 ]; then
echo -n 0
else
echo -n $1
fi
}
echo "<prtg>"
# <-- Start
for i in "${array[#]}"
do
echo -n " <result>
<channel>$i</channel>
<value>"
service_status $i
echo "</value>
</result>"
done
# End -->
echo "</prtg>"
exit
And this is my output.
<prtg>
<result>
<channel>50</channel>
<value>11
0</value>
</result>
<result>
<channel>60</channel>
<value>0</value>
</result>
<result>
<channel>70</channel>
<value>2
0</value>
</result>
</prtg>
Why do I get the 0 here? —
<value>2
0</value>
If I understand your comment correctly, you want to print the count. That is the value of the count column, if present in valor.txt, or 0 if the trn_hst_id in array is not in valor.txt. This should work (though not tested):
function service_status {
val=$(cat ~/valor.txt | grep $1 | gawk '{print $1}' FS="|" | sed "s/ //g")
# ^^ so you don't need to "cd" each time
# Save the value into "$val"
echo -n "${val:-0}" # If there is nothing in $val, print a 0
}
The "${val:-0}" sequence expands as "$val", if $val has text in it, or as a literal 0 otherwise. If the $1 wasn't in valor.txt, $val will be empty, so you will get a zero. See the wiki for more about how :- and friends work.
The "0" is the result of the echo -n 0 which is executed inside the function in case the awk command works properly (which is usually the case).
From the code it is not clear why is it written like it is. It's clear that is supposed to extract certain values from a file, but the 'if' condition seems to be checking the wrong thing, the return code of 'sed' which I bet is not what is intended. (better candidate would be the return code of 'grep'.
So I would write the function like this:
function service_status {
cd
var=$(cat valor.txt | grep $1 | gawk '{print $1}' FS="|" | sed "s/ //g")
if [ -z "$var" ]; then
echo -n 0
else
echo -n "$var"
fi
}
The variable 'var' will contain the result of the "search command". If the search would not return any value then 'var' will be empty and '0' will be the output of the function, otherwise the content of 'var' will be on the output.
I have this script:
#!/bin/bash
ping_1=$(ping -c 1 www.test.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//')
ping_2=$(ping -c 1 www.test1.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//')
ping_3=$(ping -c 1 www.test2.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//')
ping_4=$(ping -c 1 www.test3.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//' )
Then I would like to treat the outputs of ping_1-4 in one variable. Something like this:
#!/bin/bash
if [ "$ping_*" -gt 50 ]; then
echo "One ping is to high"
else
echo "The pings are fine"
fi
Is there a possibility in bash to read these variables with some sort of wildcard?
$ping_*
Did nothing for me.
The answer to your stated problem is that yes, you can do this with parameter expansion in bash (but not in sh):
#!/bin/bash
ping_1=foo
ping_2=bar
ping_etc=baz
for var in "${!ping_#}"
do
echo "$var is set to ${!var}"
done
will print
ping_1 is set to foo
ping_2 is set to bar
ping_etc is set to baz
Here's man bash:
${!prefix*}
${!prefix#}
Names matching prefix. Expands to the names of variables whose
names begin with prefix, separated by the first character of the
IFS special variable. When # is used and the expansion appears
within double quotes, each variable name expands to a separate
word.
The answer to your actual problem is to use arrays instead.
I don't think there's such wildcard.
But you could use a loop to iterate over values, for example:
exists_too_high() {
for value; do
if [ "$value" -gt 50 ]; then
return 0
fi
done
return 1
}
if exists_too_high "$ping_1" "$ping_2" "$ping_3" "$ping_4"; then
echo "One ping is to high"
else
echo "The pings are fine"
fi
You can use "and" (-a) param:
if [ $ping_1 -gt 50 -a \
$ping_2 -gt 50 -a \
$ping_3 -gt 50 -a ]; then
...
...
Or instead of defining a lot of variables, you can make an array and check with a loop:
pings+=($(ping -c 1 www.test.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//'))
pings+=($(ping -c 1 www.test1.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//'))
pings+=($(ping -c 1 www.test2.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//'))
pings+=($(ping -c 1 www.test3.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//' ))
too_high=0
for ping in ${pings[#]}; do
if [ $ping -gt 50 ]; then
too_high=1
break
fi
done
if [ $too_high -eq 1 ]; then
echo "One ping is to high"
else
echo "The pings are fine"
fi
To complement the existing, helpful answers with an array-based solution that demonstrates:
several advanced Bash techniques (robust array handling, compound conditionals, handling the case where pinging fails)
an optimized way to extract the average timing from ping's output by way of a single sed command (works with both GNU and BSD/macOS sed).
reporting the servers that either took too long or failed to respond by name.
#!/usr/bin/env bash
# Determine the servers to ping as an array.
servers=( 'www.test.com' 'www.test1.com' 'www.test2.com' 'www.test3.com' )
# Initialize the array in which timings will be stored, paralleling the
# "${servers[#]}" array.
avgPingTimes=()
# Initialize the array that stores the names of the servers that either took
# too long to respond (on average), or couldn't pe pinged at all.
failingServers=()
# Determine the threshold above which a timing is considered too high, in ms.
# Note that a shell variable should contain at least 1 lowercase character.
kMAX_TIME=50
# Determine how many pings to send per server to calculate the average timing
# from.
kPINGS_PER_SERVER=1
for server in "${servers[#]}"; do
# Ping the server at hand, extracting the integer portion of the average
# timing.
# Note that if pinging fails, $avgPingTime will be empty.
avgPingTime="$(ping -c "$kPINGS_PER_SERVER" "$server" |
sed -En 's|^.* = [^/]+/([^.]+).+$|\1|p')"
# Check if the most recent ping failed or took too long and add
# the server to the failure array, if so.
[[ -z $avgPingTime || $avgPingTime -gt $kMAX_TIME ]] && failingServers+=( "$server" )
# Add the timing to the output array.
avgPingTimes+=( "$avgPingTime" )
done
if [[ -n $failingServers ]]; then # pinging at least 1 server took too long or failed
echo "${#failingServers[#]} of the ${#servers[#]} servers took too long or couldn't be pinged:"
printf '%s\n' "${failingServers[#]}"
else
echo "All ${#servers[#]} servers responded to pings in a timely fashion."
fi
Yes bash can list variables that begin with $ping_, by using its internal compgen -v command, (see man bash under SHELL BUILTIN COMMANDS), i.e.:
for f in `compgen -v ping_` foo ; do
eval p=\$$f
if [ "$p" -gt 50 ]; then
echo "One ping is too high"
break 1
fi
[ $f=foo ] && echo "The pings are fine"
done
Note the added loop item foo -- if the loop gets through all the variables, then print "the pings are fine".
Here I'm accepting few mount points from the user and using each value to get space available on the host.
./user_input.ksh -string /m01,/m02,/m03
#!/bin/ksh
STR=$2
function showMounts {
echo "$STR"
arr=($(tr ',' ' ' <<< "$STR"))
printf "%s\n" "$(arr[#]}"
for x in "${arr[#]}"
do
free_space=`df -h "$x" | grep -v "Avail" | awk '{print $4}'`
echo "$x": free_space "$free_space"
done
#echo "$total_free_space"
}
Problems:
How can I exit for loop if any of the user input mount not avaialble?
currently it only add error in the log.
How to get total_free_space (i.e. sum of free_space)?
If you want to keep your code , test this (no ksh here). If you don't care, read Ed Morton's answer.
./user_input.ksh -string /m01,/m02,/m03
#!/bin/ksh
STR=$2
function showMounts {
echo "$STR"
arr=($(tr ',' ' ' <<< "$STR"))
printf "%s\n" "${arr[#]}"
for x in "${arr[#]}"; do
free_space=$(df -P "$x" | awk 'NR > 1 && !/Avail/{print $4}')
echo "$x: free_space $free_space"
((total_free_space+=$free_space))
done
echo "$((total_free_space/1024/1000))G"
}
showMounts
Caution:
"${arr[#]}"
not
"$(arr[#]}"
As I said in your last question, you do not need ANY of that, all you need is a one-liner like:
df -h "${STR//,/ }" | awk '/^ /{print $5, $3; sum+=$3} END{print sum}'
I have to say "like" because you haven't shown us the df -h /m01 /m02 /m03 output yet so I don't know exactly how to parse it.
How can I get this to work?
#!/bin/bash
SOMETHING=$(egrep '^ something' /some/dir/file.conf | awk -F '.' '{print $1}' | awk '{print $2}')
if [ $SOMETHING = 123 ]; then
echo "Found 123"
else
echo "Cannot find 123" && exit 1
fi
Results in grep complaining about a syntax error. It doesn't like the '^ something'
Your multiple commands with pipes can be simply replaced with the awk itself. Use following script:
SOMETHING=$(awk '/^ something/{print substr($4, 1, 3);}' somefile.conf)
if [ "$SOMETHING" = "123" ]; then
echo "Found 123"
else
echo "Cannot find 123" && exit 1
fi
EDIT: Looks like you've edited the question and your script after I posted my anser. Here is the modified awk command for you latest edit (don't do it again pls):
SOMETHING=$(awk -F "." '/^ something/{split($1, a, " "); print a[2]}' somefile.conf)