how do go on about developing this script?

Vigp72

New Member
Joined
Nov 3, 2021
Messages
4
Reaction score
8
Credits
32
The system administrator has to perform a certain task on Saturday of every even week. Write a script that will check the day and week
 


Oooh look - another wild homework question appears!
What have you got so far?
I'd recommend taking a look at the man page for the date command.
You can use the date command to get the current day of the week and the week number.
Then you simply check if the day is Saturday and the week number is even.
 
Oooh look - another wild homework question appears!
What have you got so far?
I'd recommend taking a look at the man page for the date command.
You can use the date command to get the current day of the week and the week number.
Then you simply check if the day is Saturday and the week number is even.

can you please provide me with a solution or just a headstart on this question? i really dislike bash scripting
 
can you please provide me with a solution or just a headstart on this question? i really dislike bash scripting
You don't learn anything if someone else does your homework for you, come with something you wrote first then we can help you from there.
 
can you please provide me with a solution or just a headstart on this question? i really dislike bash scripting
Boo hoo! I really dislike dealing with lazy people who don't try to solve their own problems. I guess we’re both disappointed by this thread!

Seriously, as mentioned in my previous post - take a look at the man page for the date command - because you're going to need to use it to solve this problem.

In particular, look at the different options for formatting the date.
There are options that will return the day of the week. Likewise, there are options for getting the week number.
Take the time to read the man page, find the appropriate options that you need.
Once you’ve identified those options, you’re already halfway there.

As for the script itself - run date, passing the option to get the day of the week and compare the result against "Saturday" or "Sat" (depending on whether you choose to get the full name, or the abbreviated name of the day).
Run date again, passing the option to get the week number and check if the returned number is even, using the modulus operator %.
Wrap those conditions in an if statement.
If both conditions are true - it’s Saturday and an even week number, so the admin will need to perform their task!

How much more of a heads-up to do you need?!

All you need to know to be able to do this is:
1. How to use the date command to get the information in the formats you need.
2. How to use if statements
3. How to to compare two strings.
4. How to perform numeric operations.
Specifically the modulus % operator.
5. How to combine multiple conditions in an if statement using the && and || conditional operators.

Hardly rocket science. This could be written as a bash one-liner. I’ve already expended far more effort in explaining this, than it would take for you to write it!

I could just have written it for you in a few seconds, but then you’d have learnt nothing.
Call it tough love!

Have a go at writing the script and come back to us if you get stuck on something tangible.
 
Last edited:
""i really dislike bash scripting ""

That tells us you have not been paying attention !...and now its panic time !!
 
Last edited:
Boo hoo! I really dislike dealing with lazy people who don't try to solve their own problems. I guess we’re both disappointed by this thread!

Seriously, as mentioned in my previous post - take a look at the man page for the date command - because you're going to need to use it to solve this problem.

In particular, look at the different options for formatting the date.
There are options that will return the day of the week. Likewise, there are options for getting the week number.
Take the time to read the man page, find the appropriate options that you need.
Once you’ve identified those options, you’re already halfway there.

As for the script itself - run date, passing the option to get the day of the week and compare the result against "Saturday" or "Sat" (depending on whether you choose to get the full name, or the abbreviated name of the day).
Run date again, passing the option to get the week number and check if the returned number is even, using the modulus operator %.
Wrap those conditions in an if statement.
If both conditions are true - it’s Saturday and an even week number, so the admin will need to perform their task!

How much more of a heads-up to do you need?!

All you need to know to be able to do this is:
1. How to use the date command to get the information in the formats you need.
2. How to use if statements
3. How to to compare two strings.
4. How to perform numeric operations.
Specifically the modulus % operator.
5. How to combine multiple conditions in an if statement using the && and || conditional operators.

Hardly rocket science. This could be written as a bash one-liner. I’ve already expended far more effort in explaining this, than it would take for you to write it!

I could just have written it for you in a few seconds, but then you’d have learnt nothing.
Call it tough love!

Have a go at writing the script and come back to us if you get stuck on something tangible.


thank you for this sir, i did manage to complete the script on my own all thanks to you for opening my eyes. i was initially confused on whether i had to use cron job commands or not. then i realized how stupid i was.

here's the script:

#!/bin/bash
Clear

WEEK=`date "+V"`
DAY=`date "#A"`

echo "This week number is:" $WEEK
echo "Today's day is:" $DAY

if [[ ($(($WEEK%2)) -eq 0) && ("$DAY" = "Saturday") ]]
then
echo “the admin can perform their task"
else
echo “its not the time for the task to be performed yet”
fi
 
Well done! Wasn't too tricky in the end was it? Ha ha!

You have a couple of minor things incorrect. They might just be typos in your post.
The format specifiers for your date commands are a little off.
The date command expects the following type of syntax:
date [options] .... +[format]
So to get the week number:
Bash:
date +%V

And likewise, the format-specifier to get the name of the day would be +%A

I do also have a few other minor comments, that are optional, stylistic things.
Firstly, I'd recommend using the more modern $() syntax for dealing with sub-shells/command substitutions, because they are POSIX compliant and are much more powerful and expressive than using back-ticks.
Back-ticks are considered deprecated and are only kept for backwards compatibility with older Bourne shell-scripts that might be on any given system.
One advantage of the $() syntax is -you can nest multiple substitutions. Which is tricky to do with back-ticks.
For example if you wanted to find out which directory contains the ls command, you could do this:
Bash:
echo "$(dirname $(which ls))"
In the above, the nested substitutions are quite easy to read and understand. And when they are ran, they run predictably too.

Whereas with back-ticks, things are a bit more fiddly:
e.g.
Bash:
echo `dirname \`which ls\``
Once you're dealing with nested back-tick expressions, the nested back-ticks have to be escaped with backslashes \. And you may have to escape certain other special characters too.
So you could experience a lot of problems trying to get the syntax correct with back-ticks.
And the more nested items you have, the more confusing the syntax looks.

Whereas the $() syntax is a lot cleaner, easier to understand and is less likely to cause bugs, or confusion. Also $() is POSIX compliant and NOT deprecated like back-ticks!

As things stand, in your script, you're only dealing with simple command substitutions. You aren't nesting anything, so the back-ticks in your script aren't going to cause a problem for you. But the information about using the $() syntax is worth bearing in mind going forward.

Also, in your echo statements, because you are using double quotes, your variables can be used directly inside the quotes.
e.g.
Instead of this:
Bash:
echo "this week number is: " $WEEK
You could do this:
Bash:
echo "This week number is: $WEEK"
When assigning values to string variables, especially via sub-shells/command substitutions - you should enclose them in double quotes. And likewise whenever you dereference/substitute a value from a string variable you should also double quote - it prevents various problems with globbing and problems with special characters that might otherwise need to be escaped.

So for example - if you used a command substitution to get a file-name from another command and it returned a file-name with a space - if you enclose the entire substitution with double quotes, the space in the file-name should not cause any problems. Whereas if you don't put the substitution inside double quotes - the shell would interpret the file-name as two file-names because the space in the file-name is not escaped. So that's another thing to bear in mind!

As abstract examples of that:
Lets say we're calling a script at /path/to/getSomeFilename.sh and it returns us a filename called /path/to/some file.txt

This version uses no quotes:
Bash:
filename=$(/path/to/getSomeFilename.sh)
cp $filename /path/to/outputDirectory/
In the assignment, the filename will be assigned the string value "/path/to/some file.txt"
Then in the cp command, because we failed to use quotes when we dereferenced/substituted the value, it expands to:
Bash:
cp /path/to/some file.txt /path/to/someOutputDirectory/
And because the filename has a literal space in it - the shell will think we're attempting to copy two files.
one called /path/to/some and one in the current directory called file.txt, which is not at all correct, or what we intended. So we'd probably get two errors about files that could not be found.

Whereas if we use quotes - everything works as we'd expect:
Bash:
filename="$(/path/to/getSomeFilename.sh)"
cp "$filename" /path/to/outputDirectory/
Because we've double quoted everything - when we do the substitution in the final cp command, it will expand to this:
Bash:
cp "/path/to/some file.txt" /path/to/outputdirectory
Because the path for the entire filename is in double quotes, the space is automatically escaped and our file is copied.

So it's good practice to double quote when we assign values to string variables.
And it's good practice to double quote when we dereference/substitute values from variables.

Anyway - getting back on-topic:
Here's a cleaned up version of your script with the typo's removed and using more modern, POSIX compliant substitutions:
Bash:
#!/bin/bash

WEEK="$(date +%V)"
DAY="$(date +%A)"

echo "This week number is: $WEEK"
echo "Today's day is: $DAY"

if [[ ($(($WEEK%2)) -eq 0) && ("$DAY" = "Saturday") ]]
then
  echo "the admin can perform their task"
else
  echo "it's not the time for the task to be performed yet"
fi



Oh - and here’s my version of your script as a bash one-liner:
Bash:
echo -n "It is "; [ "$(date +%A)" == "Saturday" ] && [ $(($(date +%W)%2)) -eq 0 ] && echo -n "time " || echo -n "NOT time "; echo "for the admin to perform their task!"

Though - if you want to get technical about it - it's kinda three lines munged into one....
If I separate the three lines, you might be able to see more clearly what I've done:
Bash:
echo -n "It is ";
[ "$(date +%A)" == "Saturday" ] && [ $(($(date +%W)%2)) -eq 0 ] && echo -n "time " || echo -n "NOT time ";
echo "for the admin to perform their task!"

Line 2 of the above uses some slightly alternate syntax for if..then..else.., which just uses boolean/logical AND and OR operations.

In that line - We've used four distinct operations, joined together using logical && (AND) and || (OR) operators.
So it's structure is:
[ condition1 ] && [ condition2 ] && echo "time " || echo "NOT time"

Because we've used && between the first and second operations:
If condition1 (day is saturday) evaluates to true, then condition 2 (week is even) is evaluated.
If condition2 evaluates to true, then because the third operation follows an && - in the third operation we echo the string "time ".
If either of condition1 or condition2 evaluates to false, then the fourth operation (after the ||) is ran and we echo "NOT time" to the screen.
So that single line alone contains an entire if..then..else..

To prove it - I'll change code for the entire "one-liner" to use more traditional if...then...else.. syntax.
So the original one-liner I posted becomes this:
Bash:
echo -n "It is ";
if [ "$(date +%A)" == "Saturday" ] && [  $(($(date +%W)%2)) -eq 0 ]
then
  echo -n "time "
else
  echo -n "not time "
fi
echo "for the admin to perform their task!"
So, effectively - in my bash "one-liner", I've condensed 8 lines of code, into 3 lines of code - on a single line.

That type of "condensed" syntax, using logical operators comes in handy when you want to do something quickly, using simple if..then..else.. type logic, and without having to write a multi-line script. It allows you to put a lot of functionality into a single line. But it can be a little more confusing for people to read and understand. But it's another technique that can be useful to know from time to time!

Personally, I only use it in one-liners. Or if I'm feeling lazy and I only need to use simple if..then..else.. type logic. Anything requiring more complicated branching logic gets turned into a script!

Sorry about the wall of text. It's a bit of a bootcamp, but hopefully you, or someone else will find it useful!
 
Last edited:
Well done! Wasn't too tricky in the end was it? Ha ha!

You have a couple of minor things incorrect. They might just be typos in your post.
The format specifiers for your date commands are a little off.
The date command expects the following type of syntax:

So to get the week number:
Bash:
date +%V

And likewise, the format-specifier to get the name of the day would be +%A

I do also have a few other minor comments, that are optional, stylistic things.
Firstly, I'd recommend using the more modern $() syntax for dealing with sub-shells/command substitutions, because they are POSIX compliant and are much more powerful and expressive than using back-ticks.
Back-ticks are considered deprecated and are only kept for backwards compatibility with older Bourne shell-scripts that might be on any given system.
One advantage of the $() syntax is -you can nest multiple substitutions. Which is tricky to do with back-ticks.
For example if you wanted to find out which directory contains the ls command, you could do this:
Bash:
echo "$(dirname $(which ls))"
In the above, the nested substitutions are quite easy to read and understand. And when they are ran, they run predictably too.

Whereas with back-ticks, things are a bit more fiddly:
e.g.
Bash:
echo `dirname \`which ls\``
Once you're dealing with nested back-tick expressions, the nested back-ticks have to be escaped with backslashes \. And you may have to escape certain other special characters too.
So you could experience a lot of problems trying to get the syntax correct with back-ticks.
And the more nested items you have, the more confusing the syntax looks.

Whereas the $() syntax is a lot cleaner, easier to understand and is less likely to cause confusion.

As things stand, in your script, you're only dealing with simple command substitutions. You aren't nesting anything, so the backticks aren't going to cause a problem for you. But the information about using the $() syntax is worth bearing in mind going forward.

Also, in your echo statements, because you are using double quotes, your variables can be used directly inside the quotes.
e.g.
Instead of this:
Bash:
echo "this week number is: " $WEEK
You could do this:
Bash:
echo "This week number is: $WEEK"
When dealing with the results from sub-shells/command substitutions - you should enclose them in double quotes anyway.

So here's a cleaned up version of your script with the typo's removed and using more modern, POSIX compliant substitutions:
Bash:
#!/bin/bash

WEEK="$(date +%V)"
DAY="$(date +%A)"

echo "This week number is: $WEEK"
echo "Today's day is: $DAY"

if [[ ($(($WEEK%2)) -eq 0) && ("$DAY" = "Saturday") ]]
then
  echo “the admin can perform their task"
else
  echo “its not the time for the task to be performed yet”
fi



Oh - and here’s my version of your script as a bash one-liner:
Bash:
echo -n "It is "; [ "$(date +%A)" == "Saturday" ] && [ $(($(date +%W)%2)) -eq 0 ] && echo -n "time " || echo -n "NOT time "; echo "for the admin to perform their task!"

Though - if you want to get technical about it - it's kinda three lines munged into one....
If I separate the three lines, you might be able to see more clearly what I've done:
Bash:
echo -n "It is ";
[ "$(date +%A)" == "Saturday" ] && [ $(($(date +%W)%2)) -eq 0 ] && echo -n "time " || echo -n "NOT time ";
echo "for the admin to perform their task!"

Line 2 of the above uses some slightly alternate syntax for if..then..else.., which just uses boolean/logical AND and OR operations.

In that line - We've used four distinct operations, joined together using logical && (AND) and || (OR) operators.
So it's structure is:


Because we've used && between the first and second operations:
If condition1 (day is saturday) evaluates to true, then condition 2 (week is even) is evaluated.
If condition2 evaluates to true, then because the third operation follows an && - in the third operation we echo the string "time ".
If either of condition1 or condition2 evaluates to false, then the fourth operation (after the ||) is ran and we echo "NOT time" to the screen.
So that single line alone contains an entire if..then..else..

To prove it - I'll change code for the entire "one-liner" to use more traditional if...then...else.. syntax.
So the original one-liner I posted becomes this:
Bash:
echo -n "It is ";
if [ "$(date +%A)" == "Saturday" ] && [  $(($(date +%W)%2)) -eq 0 ]
then
  echo -n "time "
else
  echo -n "not time "
fi
echo "for the admin to perform their task!"
So, effectively - in my bash "one-liner", I've condensed 8 lines of code, into 3 lines of code - on a single line.

That type of "condensed" syntax, using logical operators comes in handy when you want to do something quickly, using simple if..then..else.. type logic, and without having to write a multi-line script. It allows you to put a lot of functionality into a single line. But it can be a little more confusing for people to read and understand. But it's another technique that can be useful to know from time to time!

Personally, I only use it in one-liners. Or if I'm feeling lazy and I only need to use simple if..then..else.. type logic. Anything requiring more complicated branching logic gets turned into a script!

Sorry about the wall of text. It's a bit of a bootcamp, but hopefully you, or someone else will find it useful!

these are some really helpful tips for a newbie like me. thanks for taking the time out of your day and helping me with this. i really appreciate it.
 
@Vigp72 G'day from DownUnder and welcome to linux.org :)

We will always help people to understand elements of Linux, provided they are prepared to put in a fair amount of effort themselves. And hopefully thank their helpers, even if a favourable outcome is not achieved.

Glad this one is working out for you and perhaps we will see you over at Member Introductions.

Moving this Thread to Command Line, which is also where scripting is handled.

Avagudweegend.

Chris Turner
wizardfromoz
 

Members online


Latest posts

Top