Bash for loop error - bash

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.

Related

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 =

shell commands won't run in bash script

I'm trying to pass a parameter to a bash script:
"words.sh surf"
#!/bin/bash
#words.sh
#purpose:
#
# to return a list of words
# sorted by the length of each word
#
i=$1
cat ~/wordlist | grep $i | awk '{ print length(),$0 | "sort -n" }'
and I'm getting this message:
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]].
awk: syntax error at source line 1
context is.
{ print length(), >>> -/bin/bash <<< | "sort -n" }
awk: illegal statement at source line 1
if I run the statement as a command :
cat ~/wordlist | grep surf | awk '{ print length(),$0 | "sort -n" }'
I get the correct output:
...
...
12 hypersurface
12 surfboarding
12 undersurface
12 unsurfeiting
13 subtersurface
What am I doing wrong here?
You said it's working for you, but trust me it's got issues. Re-write it as:
awk -v i="$1" '$0 ~ i{print length(), $0 }' | sort -n

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

What's the Error?

I'm unable to detect the cause of error.
Please help point me out and it's corrective action. I'm a beginner and so this will be a great help to me.
mapping.txt:
test1 hello
test2 world
My Sh file:
parameter 1 = hello
a= cat mapping.txt | grep $1 | awk '{print$1}'
echo $a
## Extracting Dump name
b=$(ls -ltr /home/oracle/$a/$1*.dmp | awk '{print $9}' | tail -1)
I'm getting test1 as echo, but i'm unable to substitute in /home/oracle/$a/$1*.dmp
script
output: ls: /home/oracle//hello*.dmp: No such file or directory
What change should i do to let it substitute as: /home/oracle/test1/hello*.dmp
From the error, you can see the variable a is not set:
Change your first line from:
a= cat mapping.txt | grep $1 | awk '{print$1}'
to:
a=$(cat mapping.txt | grep $1 | awk '{print$1}')

No output when using awk inside bash script

My bash script is:
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=echo"$output" | awk '{print $1}'
echo $score
The above script prints just a newline in my console whereas my required output is
$ curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*
?)<\/title>.*/\1/p' | awk '{print $1}'
SA
So, why am I not getting the output from my bash script whereas it works fine in terminal am I using echo"$output" in the wrong way.
#!/bin/bash
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=$( echo "$output" | awk '{ print $1 }' )
echo "$score"
Score variable was probably empty, since your syntax was wrong.

Resources