LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script question (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-question-4175457019/)

jason13131414 04-05-2013 04:24 PM

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.

buttugly 04-05-2013 06:44 PM

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......

linosaurusroot 04-05-2013 06:49 PM

Quote:

Originally Posted by jason13131414 (Post 4925904)
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

NevemTeve 04-05-2013 11:39 PM

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

VAR=$(cmd)

kooru 04-06-2013 02:55 AM

Hi and welcome to LQ!

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc5

jason13131414 04-11-2013 09:07 AM

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 (Post 4925973)
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!

w1k0 04-11-2013 04:59 PM

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]}


Jman-NZ 04-11-2013 09:07 PM

#!/bin/bash
DAY=`date +%A`
if [ $DAY == "Friday" ];
then
echo "Today's the day";
a=xxxxx;
else
a=yyyy;
fi

jason13131414 04-12-2013 09:37 AM

Thank you all! problem solved!!

David the H. 04-14-2013 08:39 AM

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.

w1k0 04-14-2013 12:24 PM

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.

chrism01 04-14-2013 06:03 PM

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. :)

w1k0 04-14-2013 09:39 PM

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.

konsolebox 04-15-2013 04:33 AM

Quote:

Originally Posted by w1k0 (Post 4931418)
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.

w1k0 04-15-2013 11:25 AM

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.


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