LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-05-2013, 04:24 PM   #1
jason13131414
LQ Newbie
 
Registered: Apr 2013
Posts: 8

Rep: Reputation: Disabled
Bash Script question


Hello World!

I squeezed all my brain juice on a script that can check for the day of the week, and then assign a value to a certain day.

For example,

if date+ %A returns as "Monday" then a value for the variable of Monday will be assigned

I thought about conditional statements, but can't make up a good logic.

Any expert out there have an idea?

Please ask me if my question is confusing.
 
Old 04-05-2013, 06:44 PM   #2
buttugly
Member
 
Registered: Feb 2006
Distribution: distrohopper
Posts: 58

Rep: Reputation: 16
My Google-fu came up with these potentials;

http://www.servernoobs.com/bash-scri...and-variables/

http://www.cyberciti.biz/faq/bash-tc...y-day-of-week/

http://stackoverflow.com/questions/8...ek-of-variable

Don't write off the first one too quickly, scroll down for days and what to do on each one.....

FWIW-HTH

Kevin

P.S. What are you ultimately trying to do? Just curious......
 
Old 04-05-2013, 06:49 PM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jason13131414 View Post
squeezed all my brain juice on a script
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

http://r-101.blogspot.co.uk/2006/09/...uotations.html
 
1 members found this post helpful.
Old 04-05-2013, 11:39 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Sorry, but what is the actual question again? How to put the output of a command into a variable?

VAR=$(cmd)
 
Old 04-06-2013, 02:55 AM   #5
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
Hi and welcome to LQ!

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc5
 
Old 04-11-2013, 09:07 AM   #6
jason13131414
LQ Newbie
 
Registered: Apr 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you all for taking the time to reply.

I should had made the question more clear. Sorry about that.

I am trying to make a script that can check for the day of the week, and then assigned that day to a specific variable.

So for example,

if [ $date = "Friday" ]; then
a=xxxxxxxx
else
xxxxxxx
fi

I just can't think of a loop that checks for the day of the week and then stops when the loop valid on whatever date+ %A's date is.

And each day have its own variables, so there are 7 variables, one for each weekdays.

Sorry if I still cause confusion.

---------- Post added 04-11-13 at 10:08 AM ----------

Quote:
Originally Posted by linosaurusroot View Post
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

http://r-101.blogspot.co.uk/2006/09/...uotations.html
well do, this is a good tips for my future in programming. Thanks a ton!

Last edited by jason13131414; 04-11-2013 at 09:09 AM.
 
Old 04-11-2013, 04:59 PM   #7
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
Code:
#!/bin/bash

# the following is for Monday=1 ... Sunday=7

day=$(date +"%u")
declare -i n=1

# for Sunday=0 ... Saturday=6 use the following

# day=$(date +"%w")
# declare -i n=0

for number in 123 234 345 456 567 678 789
do
    number[$n]=$number
    n=$n+1
done

echo ${number[$day]}
 
1 members found this post helpful.
Old 04-11-2013, 09:07 PM   #8
Jman-NZ
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Rep: Reputation: Disabled
#!/bin/bash
DAY=`date +%A`
if [ $DAY == "Friday" ];
then
echo "Today's the day";
a=xxxxx;
else
a=yyyy;
fi
 
1 members found this post helpful.
Old 04-12-2013, 09:37 AM   #9
jason13131414
LQ Newbie
 
Registered: Apr 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you all! problem solved!!
 
Old 04-14-2013, 08:39 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

And can we please get the quoting syntax right?

Code:
#!/bin/bash

DAY=$( date +%A )

if [ "$DAY" = Friday ]; then
    echo "Today's the day."
    a=xxxxx
else
    a=yyyy
fi
Although when using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression


Using "[[..]]" makes quoting unnecessary (although it doesn't hurt to have them anyway).

Also, $(..) is highly recommended over `..`. Please don't use backticks in modern shells.
 
Old 04-14-2013, 12:24 PM   #11
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
David the H.,

I see the similar post written by you for the sixth time during last two weeks. It seems that you have a mission from God to popularize the right methods of quoting and formatting the posts. jason13131414 published here 9 posts so far. It takes some time before a newbie grasps all the rules. Would you like to educate each newbie here? Put a short tutorial into the signature of your posts. Do not post the same content for the dozens of the times because it is boring. Each day a bunch of newbies join LinuxQuestions.org. Maybe you should write a short tutorial, send it to jeremy, and ask him to post it to the every new LQ member? It seems reasonable solution.

On the other hand there are a lot of the ways of the formatting the posts which are acceptable. I – for example – put the content of the files (data files and scripts) into the code section and I use bold font to mark the files and directories names as well as the separate commands (the commands which are not the part of the scripts). It is not reasonable to expect that in the forum used by the thousands of the users each of them will use exactly the same style of the formatting of the posts.

As for the bash techniques the most important is the difference between the working and not working ones. The backticks and the single brackets work so they are valid ones. Using or avoiding them is a matter of the style of the coding. You – and the other people – can call some coding styles better than the other ones but it is only a matter of the taste. So it is not reasonable to expect that each coder will share your taste.
 
Old 04-14-2013, 06:03 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
There are very good code/logic reasons (not just style) for using [[ ]] instead of 'test' or [ ].
Please read the FAQ link provided and http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
Here's a quote (there's more) from that link
Quote:
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
I was told to use [[ ]] on a ksh course at HP many years ago (& always do), and it has proven to be wise advice.

Best coding (nothing to do with style) practice will avoid many pitfalls and that is what the more experienced member try to pass on to the newer members.
If you want to ignore any advice seen on the forum, that is of course your prerogative.
 
Old 04-14-2013, 09:39 PM   #13
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
chrism01,

I do not ignore the advices. I just stated that it is boring to read the same advice for the next time. Maybe each newbie could get the post including that stuff. As for [ ... ] the && and || operators work outside of that and -lt and -gt operators work inside. It is just the other style of the coding. I do not see here a challenge to go to a holy war.
 
Old 04-15-2013, 04:33 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by w1k0 View Post
The backticks and the single brackets work so they are valid ones.
They do but their capabilities are limited. You can't recurse backticks.
Code:
echo "`echo "`echo a`"`"
echo `echo `echo a``
Quote:
As for [ ... ] the && and || operators work outside of that and -lt and -gt operators work inside.
But you can't group conditions.
Code:
[[ CONDITION && (CONDITION || CONDITION) ]]
And you can't use expressions.
 
Old 04-15-2013, 11:25 AM   #15
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
konsolebox,

One post – two lies.

Code:
#!/bin/bash

echo "Hello"
echo "Hello $(echo the H.)"
echo "Hello $(echo David $(echo the H.))"

echo

echo "Hello"
echo "Hello `echo k0`"
echo "Hello `echo w1\`echo k0\``"

echo

if [[ 2 > 1 && (1 > 0 || 1 < 0) ]]
then
    echo "Well done David the H."
fi

echo

if [ 2 -gt 1 ] && ( [ 1 -gt 0 ] || [ 1 -lt 0 ] )
then
    echo "Well done w1k0"
fi
***

It seems that we stole the thread by jason13131414.
 
  


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
[bash-script] A question about using grep in the script thomas2004ch Linux - Software 2 03-05-2012 03:27 AM
Bash script question stillme Programming 15 05-09-2010 09:56 AM
Bash script question.... mkhan919 Linux - Newbie 3 04-06-2007 06:13 AM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 02:13 AM

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

All times are GMT -5. The time now is 04:38 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