LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 08-12-2011, 04:34 AM   #1
QueenZ
Member
 
Registered: Sep 2008
Distribution: openSUSE, Ubuntu
Posts: 373
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?

Code:
#!/bin/bash
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...

Quote:
./startchrome.sh: line 2: dayofweek: command not found
/home/queenz/Documents/grdayz/.png
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, 05:04 AM   #2
dimothy
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.

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

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

Delete the spaces around the = for assigning a value to a variable. You should also include your commands in a subshell like this:
Code:
dayofweek=$(date | awk '{ print substr( $0, 0, 4 ) }')
Have a look at these: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html
http://tldp.org/LDP/abs/html/varsubn.html

Kind regards,

Eric
 
Old 08-12-2011, 06:26 AM   #4
guna_pmk
Member
 
Registered: Sep 2008
Posts: 220

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

dayofweek=$(date +'%a')

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

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

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


Reply


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 06:07 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:57 AM.

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