Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
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]}
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.
#!/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.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
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.
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.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.