Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-12-2011, 05:34 AM   #1
Registered: Sep 2008
Distribution: openSUSE, Ubuntu
Posts: 372
Blog Entries: 2

Rep: Reputation: 32
What is wrong with my 3 line shell script?

I wanted to get the date so I used this shell script but the variable $dayofweek is empty.. why?

dayofweek = date | awk '{ print substr( $0, 0, 4 ) }'
exec eog /home/queenz/Documents/grdayz/$dayofweek.png &
echo /home/queenz/Documents/grdayz/$dayofweek.png
the output is...

./ line 2: dayofweek: command not found
if I run date | awk '{ print substr( $0, 0, 4 ) }' in terminal it outputs "Fri" which is good but it doesn't work in my script...
Old 08-12-2011, 06:04 AM   #2
LQ Newbie
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
I am pretty sure it is the spaces you have around the variable. remove those and you should get a result.

dayofweek=date | awk '{ print substr( $0, 0, 4 ) }'
Old 08-12-2011, 06:04 AM   #3
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295

Delete the spaces around the = for assigning a value to a variable. You should also include your commands in a subshell like this:
dayofweek=$(date | awk '{ print substr( $0, 0, 4 ) }')
Have a look at these:

Kind regards,

Old 08-12-2011, 07:26 AM   #4
Registered: Sep 2008
Posts: 220

Rep: Reputation: 5
Just an easier method (I think)

dayofweek=$(date +'%a')

1 members found this post helpful.
Old 08-12-2011, 09:00 AM   #5
Senior Member
Registered: Jan 2010
Posts: 1,665

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Originally Posted by QueenZ View Post
dayofweek = date | awk '{ print substr( $0, 0, 4 ) }'
exec eog /home/queenz/Documents/grdayz/$dayofweek.png &
echo /home/queenz/Documents/grdayz/$dayofweek.png

if you really want to use 'awk' for this then why not use awk's specialty to address fields in a line?
date | awk '{ print $1 }'
guna's solution is even better suited for this.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 10:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 09:49 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 09:41 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:48 PM.

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