New Posts  All Forums:Forum Nav:

BASH scripting help!

post #1 of 5
Thread Starter 
Okay, yesterday was my first day in my Linux Scripting class and we went through alot of stuff (kinda) and im a wee bit confused about how to complete this assignment.
My task

Have the following displayed whenever a new shell is invoked.

Today is <day of week> the <day of month>th of <month>

I understand everything except for the "th" part at the end of the Day of month. Im trying to use an If-Then statement to make it so it will change depending on the number of the day (i.e. 1st, 3rd, 5th)

So my code for the statement without the if-then part is
Quote:
day=`date +%A`
month=`date +%B`
dom=`date +%d`

echo "Today is" $day "the" $dom"th of" $month
Which yeilds
Quote:
Today is Wednesday the 19th of October


And after that im lost.. i typed out a bit of code, but i dont know if im anywhere close, i referenced it from an example. It looks like this
Quote:
str1="1,21,31"
str2="2,22"
str3="3,23"
srt4="4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 ,24,25,26,27,28,29,30"

if [[ $dom == $str1 ]]
then
echo "st"
else
if [[ $dom == $str2 ]]
then
echo "nd"
else
if [[ $dom == $str3 ]]
then
echo "rd"
else
if [[ $dom == $str4 ]]
then
echo "th"
Is this the right way to do an If-Then statement to get the "th" to change to a "nd, rd"?

If so, how do i get it into my original code
Edited by 10halec - 10/19/11 at 9:36pm
Coming Soon!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K ASRock P67 EXTREME4 GEN3 Sapphire Toxic HD 6950 2GB G.SKILL Ripjaws X Series 8GB (2 x 4GB) 
Hard DriveOSCase
Crucial m4 128gb (boot/games), SAMSUNG F3 1tb Windows 7 Ultimate, Fedora 14 Corsair 650D 
  hide details  
Reply
Coming Soon!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K ASRock P67 EXTREME4 GEN3 Sapphire Toxic HD 6950 2GB G.SKILL Ripjaws X Series 8GB (2 x 4GB) 
Hard DriveOSCase
Crucial m4 128gb (boot/games), SAMSUNG F3 1tb Windows 7 Ultimate, Fedora 14 Corsair 650D 
  hide details  
Reply
post #2 of 5
I can't say if your If...then syntax is correct, it's a long time since I used BASH. However, assuming it is correct, instead of using "echo" inside the if statements , set a variable, say "dom_suffix". Now place that chunk of code before:

Code:
echo "Today is" $day "the" $dom"th of" $month
and use "dom_suffix" in that final echo, eg.

Code:
echo "Today is" $day "the" $dom $dom_suffix " of " $month
The final code will look something like this:

Code:
day=`date +%A`
month=`date +%B`
dom=`date +%d`

str1="1,21,31"
str2="2,22"
str3="3,23"
srt4="4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 ,24,25,26,27,28,29,30"

if [[ $dom == $str1 ]]
then
$dom_suffix = "st"
else
if [[ $dom == $str2 ]]
then
$dom_suffix = "nd"
else
if [[ $dom == $str3 ]]
then
$dom_suffix = "rd"
else
if [[ $dom == $str4 ]]
then
$dom_suffix = "th"

echo "Today is" $day "the" $dom $dom_suffix" of" $month
Ah, I just noticed. Even if the if statement syntax is correct, the statements won't work as intended. You need to use arrays for str1-4 and use a function along the lines of "isinarray()" to test if $dom is contained within the array.
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #3 of 5
Code:
#!/bin/bash

dayofmonth=$1
let temp=$dayofmonth%10

if [[ $temp == 1 && $dayofmonth != 11 ]];then
  echo "${dayofmonth}st"
elif [[ $temp == 2 && $dayofmonth != 12 ]];then
  echo "${dayofmonth}nd"
elif [[ $temp == 3 && $dayofmonth != 13 ]];then
  echo "${dayofmonth}rd"
else
  echo "${dayofmonth}th"
fi
The New Guy
(16 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0@3.0GHz GA-P35-DS3L EVGA 8800GT Superclocked 4GB Corsair XMS2 DDR2 800 
Hard DriveHard DriveOptical DriveOS
Seagate 320GB 7200RPM Western Digital Caviar Black Lite-on DVD burner Windows 7 Professional x64 
MonitorKeyboardPowerCase
Samsung 225BW Filco Majestouch Ninja (full keyboard w/browns) Cooler Master Xtreme 650 Antec 900 
MouseAudioAudio
Logitech MX518 Denon AHD-2000 Sennheiser HD595 
  hide details  
Reply
The New Guy
(16 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0@3.0GHz GA-P35-DS3L EVGA 8800GT Superclocked 4GB Corsair XMS2 DDR2 800 
Hard DriveHard DriveOptical DriveOS
Seagate 320GB 7200RPM Western Digital Caviar Black Lite-on DVD burner Windows 7 Professional x64 
MonitorKeyboardPowerCase
Samsung 225BW Filco Majestouch Ninja (full keyboard w/browns) Cooler Master Xtreme 650 Antec 900 
MouseAudioAudio
Logitech MX518 Denon AHD-2000 Sennheiser HD595 
  hide details  
Reply
post #4 of 5
Thread Starter 
Quote:
Originally Posted by Desidero View Post
Code:
#!/bin/bash

dayofmonth=$1
let temp=$dayofmonth%10

if [[ $temp == 1 && $dayofmonth != 11 ]];then
  echo "${dayofmonth}st"
elif [[ $temp == 2 && $dayofmonth != 12 ]];then
  echo "${dayofmonth}nd"
elif [[ $temp == 3 && $dayofmonth != 13 ]];then
  echo "${dayofmonth}rd"
else
  echo "${dayofmonth}th"
fi

How do i incorporate that into the ending echo? I cant seem to figure it out?
Coming Soon!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K ASRock P67 EXTREME4 GEN3 Sapphire Toxic HD 6950 2GB G.SKILL Ripjaws X Series 8GB (2 x 4GB) 
Hard DriveOSCase
Crucial m4 128gb (boot/games), SAMSUNG F3 1tb Windows 7 Ultimate, Fedora 14 Corsair 650D 
  hide details  
Reply
Coming Soon!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K ASRock P67 EXTREME4 GEN3 Sapphire Toxic HD 6950 2GB G.SKILL Ripjaws X Series 8GB (2 x 4GB) 
Hard DriveOSCase
Crucial m4 128gb (boot/games), SAMSUNG F3 1tb Windows 7 Ultimate, Fedora 14 Corsair 650D 
  hide details  
Reply
post #5 of 5
I won't give you the answer, but here's a hint:


$1 would be the first argument when you call the script.

i.e. ./myscript.sh 5
$1 would be 5

I probably already gave you too much information considering this is your homework... Hopefully this is enough for you to complete it.
The New Guy
(16 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0@3.0GHz GA-P35-DS3L EVGA 8800GT Superclocked 4GB Corsair XMS2 DDR2 800 
Hard DriveHard DriveOptical DriveOS
Seagate 320GB 7200RPM Western Digital Caviar Black Lite-on DVD burner Windows 7 Professional x64 
MonitorKeyboardPowerCase
Samsung 225BW Filco Majestouch Ninja (full keyboard w/browns) Cooler Master Xtreme 650 Antec 900 
MouseAudioAudio
Logitech MX518 Denon AHD-2000 Sennheiser HD595 
  hide details  
Reply
The New Guy
(16 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0@3.0GHz GA-P35-DS3L EVGA 8800GT Superclocked 4GB Corsair XMS2 DDR2 800 
Hard DriveHard DriveOptical DriveOS
Seagate 320GB 7200RPM Western Digital Caviar Black Lite-on DVD burner Windows 7 Professional x64 
MonitorKeyboardPowerCase
Samsung 225BW Filco Majestouch Ninja (full keyboard w/browns) Cooler Master Xtreme 650 Antec 900 
MouseAudioAudio
Logitech MX518 Denon AHD-2000 Sennheiser HD595 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming