LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2018, 02:02 PM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Rep: Reputation: Disabled
Question Variable doesn't work on a simple bash script


Hello

I am using this command:

Code:
awk '($9 ~ /307/)' /home/nginx/domains/mydomain.com/log/access.log | awk '{print $7}' | sort | uniq -c | sort -rn
and i am getting an output like this:

Code:
      7 /login/
      3 /login
      1 /login/?ref=a
Now i was trying to create a bash script for it like this:

Code:
#!/bin/bash

echo Please select the error code:
read errorcode

awk '($9 ~ /${errorcode}/)' /home/nginx/domains/mydomain.com/log/access.log | awk '{print $7}' | sort | uniq -c | sort -rn
as an error i am typing 307 but it doesn't work as i get a blank result

When i use echo and the variable it works...

I was trying to set also the variable in quotes but it was not working ....

Anyone knows how to do that?

Thank you

Last edited by bmxakias; 07-30-2018 at 02:03 PM.
 
Old 07-30-2018, 02:06 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
awk '($9 ~ /'"$errorcode"'/)' filename
 
1 members found this post helpful.
Old 07-30-2018, 02:32 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
The -v option in AWK brings an environment variable into the program:

Code:
#!/bin/bash

echo Please select the error code:
read errorcode

awk -v e=${errorcode} '$9 ~ e { print $7 }' /home/nginx/domains/mydomain.com/log/access.log \
        | sort | uniq -c | sort -k1,1rn
AWK variables are just plain strings and not preceded by a dollar sign.

See "man awk" for the details on the version of AWK you have.
 
1 members found this post helpful.
Old 07-30-2018, 03:22 PM   #4
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Thank you !
 
Old 07-30-2018, 04:36 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
sort | uniq -c | sort -k1,1rn
Are you sure you need all this, isn't the last sort enough?
 
Old 07-31-2018, 01:24 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by keefaz View Post
Code:
sort | uniq -c | sort -k1,1rn
Are you sure you need all this, isn't the last sort enough?
The uniq program needs sorted input and then using it with the -c option the counts coming out won't be in order so the output must be sorted again.
 
2 members found this post helpful.
Old 07-31-2018, 03:10 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Oh yes, I overlooked the -c option for uniq , it makes sense now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Simple bash script that reads variable from a web file reevv Programming 3 08-20-2017 08:37 AM
[SOLVED] bash: simple logic doesn't work - version checking robertjinx Programming 6 11-10-2016 12:50 AM
[SOLVED] stupid simple script doesn't work ?? sharky Linux - Software 4 01-12-2011 01:54 PM
Simple variable bash script help/question lilrazzzzz Programming 4 04-29-2009 05:50 PM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration