Excecuting shell script from chef recipe - ruby

I have to execute a script from recipe based on the result which I have to get from the ubuntu node. I have to get the version of agent running and based on that I have to execute script. Below is the condition in recipe which I am running.
notifies :run, 'bash[uninstall CloudPassage]', :immediate
only_if { Mixlib::ShellOut.new("dpkg -s cphalo | grep Version | awk '{print $2}'" -lt "3.9.5").run_command.success? }
but it is giving a syntax. can anyone help me with the syntax to get version number from ubuntu node.
Syntax error:
FATAL: Cookbook file recipes/default.rb has a ruby syntax error:
FATAL: /home/ubuntu/chef-repo/cookbooks/cloudPassage/recipes/default.rb:27:syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
FATAL: ...rsion | awk '{print $2}'" -lt "3.9.5").run_command.success? }
FATAL: ... ^
FATAL: /home/ubuntu/chef-repo/cookbooks/cloudPassage/recipes/default.rb:27: syntax error, unexpected ')', expecting '}'
FATAL: ... awk '{print $2}'" -lt "3.9.5").run_command.success? }
FATAL: ...

The correct-er way to write this:
only_if { Gem::Requirement.create('< 3.9.5').satisfied_by?(Gem::Version.create(shell_out!('dpkg -s cphalo').stdout[/^Version: (.*)$/, 1])) }
Or something like that.

This script checks if cphalo version is less than (-lt ) 3.9.5.
get the line with cphalo reading Version:
"dpkg -s cphalo | grep Version"
then pipe it to awk and print second field ($2)
| awk '{print $2}'"
the result is compared to "3.9.5" thanks to -lt
">result<" -lt "3.9.5"
which returns true or false.
But you must not break your string like you do with internal double quotes. try the below:
" $(dpkg -s cphalo | grep Version | awk '{print $2}') -lt 3.9.5 "
or if square braces are needed:
" [ $(dpkg -s cphalo | grep Version | awk '{print $2}') -lt 3.9.5 ] "

Related

Unable to get value from file

I try to get the value of "length" of the first line of different text files but I am getting this error :
")syntax error: invalid arithmetic operator (error token is "
Here is the first line of one file :
><some info> <some info> | <other info> | <otherinfo> [Source:xxxx/xxxx;xxxx:xxxx] | <some info> | length=2812
So I want to get the value 2812. After many tests, the best I can get is this :
for file in ./*.txt
do
LLINE=$(head -n 1 "$file" | awk -F "length=" '{print $NF}')
echo "${LLINE}."
if [[ "${LLINE}" -le 1500 ]];
then
<some code>
else
<some code>
fi
done
My output is :
2812
")syntax error: invalid arithmetic operator (error token is "
376
")syntax error: invalid arithmetic operator (error token is "
...
What is the issue ?
maybe grep would be better for LLINE?
grep -Eo '[0-9]{1,4}'
Finally found with all your help !
This was DOS line endings error since those files were generated by someone using windows.
I just added a sed command to get rid of this :
LLINE=$(head -n 1 "$file" | awk -F "length=" '{print $NF}' | sed $'s/\r//')

unexpected EOF while looking for matching error

I am running below script and getting
error script.sh: line 9: unexpected EOF while looking for matching `''
script.sh: line 15: syntax error: unexpected end of file.
Though I tried to run line 9 manually n it runs without error.
alias gxt="awk -F "_" '{print \$1}' test | uniq"
count = $(cat test | awk -F "_" '{print $1} | uniq | wc -l)
for i in {1..count};
do
User=$(gxt | head -n $i)
recharge=$(grep -E "$User.recharge" test| awk -F "_" '{print $3}' | xargs )
total1=( $((${recharge// /+})))
sales=$(grep -E "$User.sale" test| awk -F "_" '{print $3}' | xargs )
total2=( $((${sales// /+})))
balance=`expr $total1 - $total2`
echo $User.balance.$balance >> result
done
Other than the issues already reported and those exposed by shellcheck, there is another issue:
for i in {1..count};
'count' cannot be a variable. It can only be a constant.
Change it to
for ((i = 1; i <= count; i++)); do whatever ; done
count=$(cat test | awk -F "_" '{print $1}` | uniq | wc -l)
missing ' at the end of {print $1}
Inadvertently added spaces around =

Variable works outside function, inside function give syntax error

I've been trying to get this function to work. I'm on a 17" MacBook Pro Early 2011. Setting all the variables under "else" work great if you run them separately, they also echo properly. For some reason when I put them in the function… I get a syntax error on line 12 and
battery ()
{
BATTERYISPRESENT=`ioreg -l | grep Cycle`
if [[ $BATTERYISPRESENT != *'Cycle' ]]
then
echo "No Battery Present, Probably a desktop Mac."
else
BATTERYCYCLES=`system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}'`
BATTCURRCAP=`pmset -g batt | sed -n '2 p' | awk '{ print $2 }' | sed 's/;//g'`
BATTERYCHARGESTATUS=`system_profiler SPPowerDataType | grep "Charging" | awk '{ print $2 }'`
BATTERYISCHARGING=`system_profiler SPPowerDataType | grep -A 4 "AC Charger Information" | grep "Connected: " | awk '{ print $2 }'`
CHARGERISCONNECTED=`system_profiler SPPowerDataType | grep -A 4 "AC Charger Information" | grep "Connected: " | awk '{ print $2 }'`
echo $BATTERYCYCLES
echo $BATTCURRCAP
echo $BATTERYISCHARGING
echo $CHARGERISCONNECTED
}
The output reads:
line 12: unexpected EOF while looking for matching ``'
line 18: syntax error: unexpected end of file
Any assistance would be greatly appreciated.
A fi upon you — you're missing the fi at the end of the else.

Bash for loop error

I have a script in bash what take from LocLog a ip from collumn 8 :
#!/bin/bash
for i in $(cat /scripts/logs/LocLog | awk '{print $8}' | sort | uniq);
do
php /scripts/a.php $i;
done
The script give an error:
bash -x log
'og: line 2: syntax error near unexpected token `
'og: line 2: `for i in $(cat /scripts/logs/LocLog | awk '{print $8}' | sort | uniq);
Any ideeas?
Get rid of the semicolon at the end of the for line.

error in awk of shell script

I am getting the below error ith my code.What is missing in it? My goal is to print 13.0.5.8 in $version
#!/bin/ksh
file="abc_def_APP_13.0.5.8"
if echo "$file" | grep -E "abc_def_APP"; then
echo "Version found: $file"
version1=(echo $file | awk -F_ '{print $NF}' | cut -d. -f1-3)
version2=(echo $file | awk -F_ '{print $NF}' | cut -d. -f4-)
echo $version1
echo $version2
version=$version$version2
echo $version
else
echo "Version not found"
fi
Please find below the error:
./version.sh: line 7: syntax error near unexpected token `|'
./version.sh: line 7: ` version1=(echo $file | awk -F_ '{print $NF}' | cut -d. -f1-3)'
./version.sh: line 9: syntax error near unexpected token `|'
./version.sh: line 9: ` version2=(echo $file | awk -F_ '{print $NF}' | cut -d. -f4-)'
./version.sh: line 18: syntax error near unexpected token `else'
There's no need for awk at all. Just trim every character before the last underscore, like so:
file="abc_def_APP_13.0.5.8"
version="${file##*_}"
echo "$version"
See http://mywiki.wooledge.org/BashFAQ/073 for documentation on this technique, or see "parameter expansion" in bash's own docs.
To treat the last segment separately is also straightforward:
file="abc_def_APP_13.0.5.8"
version="${file##*_}" # result: 13.0.5.8
version_end="${version##*.}" # result: 8
version_start="${version%.*}" # result: 13.0.5
echo "${version_start}/${version_end}" # result: 13.0.5/8
Because this happens internally to bash, without executing any external commands (such as awk), it should be considerably faster to execute than other approaches given.
The problem is your backticks are missing $ you need to fix the following two lines like so:
version1=$(echo $file | awk -F_ '{print $NF}' | cut -d. -f1-3)
version2=$(echo $file | awk -F_ '{print $NF}' | cut -d. -f4-)
This will fix the syntactical errors. The following line doesn't make much sense as $version hasn't been initialize yet:
version=$version$version2
Did you mean:
version="${version1}.${version2}"
A side note you are using the -E option with grep but you aren't using any extended regexp features, in fact you are doing a fixed string string search so -F is more appropriate. You probably also want to use the -q option to suppress the output from grep.
Personally I would do:
file="abc_def_APP_13.0.5.8"
echo "$file" | awk '/abc_def_APP/{print "Version found: "$0;
print $4,$5,$6;
print $7;
print $4,$5,$6,$7;
next}
{print "Version not found"}' FS='[_.]' OFS=.
If you just want the version number in the variable version then why not simply:
version=$(echo "$file" | grep -o '[0-9].*')
It can all be done in a single awk command and without additional cut command. Consider following command:
read version1 version2 < <(echo $file|awk -F "[_.]" '{
printf("%s.%s.%s ", $4, $5, $6); printf("%s", $7);
for (i=8; i<=NF; i++) printf(".%s", $i); print ""}')
echo "$version1 :: $version2"
OUTPUT
13.0.5 :: 8

Resources