Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Could someone help me with my shell script

Could someone help me with my shell script
Thread Tools
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 7, 2004, 07:55 AM
 
Hi, I've just been messing about and I tried to make a shell script that will give me the full name of the present day, it doesn't work though. on wednesdy it outputs wedday. That means add2 isn't getting changed to 'nes'
I know its probably not the best way of doing it but Im just trying to learn.
Any thoughts?



#!/bin/sh
#makes a new message everyday

set `uptime`
echo "Your system has been running for $3 minutes"

set `date`
if test $1=Mon || test $1=Fri || test $1=Sun
then
add2=

elif test $1=Tue
then
add2=s

elif test $1=Thu
then
add2=rs

elif test $1=Wed
then
add2+=nes

else
add2=er


fi

add=day

echo "Today is $1$add2$add"
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Apr 7, 2004, 08:11 AM
 
hmm, i don't think there's a += operator.
Try add2=${add2}nes

also, it's more conventional to use [ ] instead of the word 'test' in shell scripts.
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 7, 2004, 09:40 AM
 
Thanks, I forgot to take the += out of the code before i posted it. I was just testing it out.
Anyone know where I can get examples of more simple shell scripts?

Mithras, do you mean I can replace 'test' with '[]' ?
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 7, 2004, 09:45 AM
 
That didn't work. The problem isn't with the wednesday statement, its with the whole structure, the line 'add2=nes' isn't even being executed. Otherwise it would have realised the += mistake. Instead it outputted wedday ie leaving add2 blank.

Cheers
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 7, 2004, 09:52 AM
 
Code:
day_of_the_week=`date +"%a"` case $day_of_the_week in Mon) real_day="Monday" ;; Tue) real_day="Tuesday" ;; Wed) real_day="Wednesday" ;; Thu) real_day="Thursday" ;; Fri) real_day="Friday" ;; Sat) real_day="Saturday" ;; Sun) real_day="Sunday" ;; esac echo "Today is $real_day"
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Apr 7, 2004, 10:29 AM
 
Or, if brevity is the goal:
echo "Today is `date '+%A'`"


Here's a good little guide that I used a bunch in first learning bash scripting:
http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html

and this is the definitive guide:
http://www.tldp.org/LDP/abs/html/
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 7, 2004, 10:57 AM
 
thanks Arkham_c, but the synthax is completely different to what I know. I wanted to do it the complex way and have different sections of the string/word.
Im unfamiliar with
;;
Tue)

for example.
Is there anyway of fixing what I have done?
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 7, 2004, 11:01 AM
 
also, what is %a ?
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 7, 2004, 01:16 PM
 
Originally posted by digiology2:
thanks Arkham_c, but the synthax is completely different to what I know. I wanted to do it the complex way and have different sections of the string/word.
Im unfamiliar with
;;
Tue)

for example.
Is there anyway of fixing what I have done?

also, what is %a ?
Yes, you can fix what you did. As an earlier poster mentioned, nobody calls test like that. Here it is, in the form most like what you did, but syntactically, correct:

Code:
#!/bin/sh today=`date +"%a"` if [ "${today}" = "Mon" ] || [ "${today}" = "Fri" ] || [ "${today}" = "Sun" ]; then add2="" elif [ "${today}" = "Tue" ]; then add2="s" elif [ "${today}" = "Thu" ]; then add2="rs" elif [ "${today}" = "Wed" ]; then add2="nes" else # Saturday add2="ur" fi add="day" echo "Today is ${today}${add2}${add}"
the "[ ]" syntax is running "test" without calling it by name. It is the generally accepted way of calling test, and is easier to read too.

The "+%a" is a strftime format string for the date command. If you read the man page for strftime, you'll see:

Code:
%A is replaced by national representation of the full weekday name. %a is replaced by national representation of the abbreviated weekday name.
There are a ton of formatting options you can pass to date to customize what it prints.

Just for the sake of completeness, here is how I would modify my earlier example using case statements to do the "complex" solution:

Code:
#!/bin/sh today=`date +"%a"` case $today in Mon|Fri|Sun) add2="" ;; Tue) add2="s" ;; Wed) add2="nes" ;; Thu) add2="rs" ;; else) add2="ur" ;; esac add="day" echo "Today is ${today}${add2}${add}"
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 8, 2004, 07:57 AM
 
Thank you! I'll be back at the unix forum again to annoy you all when I do another script!
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 8, 2004, 08:18 AM
 
sorry, what is strftime? You said that '+%a' is part of the strftime command (although it doesn't seem to act like a command, ie i wasn't able to use it in the terminal), but you don't use strftime in the script (the one done my way but fixed).

Please explain, thanks.

I'd like to do it with the few commands I know and it appears you are doing so but i still don't get %a
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 8, 2004, 10:16 AM
 
Originally posted by digiology2:
sorry, what is strftime? You said that '+%a' is part of the strftime command (although it doesn't seem to act like a command, ie i wasn't able to use it in the terminal), but you don't use strftime in the script (the one done my way but fixed).

Please explain, thanks.

I'd like to do it with the few commands I know and it appears you are doing so but i still don't get %a
strftime is a function in the "C" programming language. It is used universally in C programs to format time values into a human-readable string. The "date" command uses strftime internally to format the time in "date". The default format for date is "%a %b %e %H:%M:%S %Z %Y".

If you open a Terminal and type "man strftime" you'll see all the formatting options for strftime. You can use any of these with the command-line "date" command as follows:

date +"format"

where format is a strftime format string.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Forum Regular
Join Date: Jan 2004
Status: Offline
Reply With Quote
Apr 8, 2004, 05:48 PM
 
but it appears %a, %b ect is the same as refering to $1, $2 ect after using the set command.

I suppose its just a different way of doing things.

How would I apply a command to every visible (ie when 'ls' is typed) file in a folder, I know you can use while loops but is there some way of counting all the files in the current folder so I can control the while loop with another variable ie

while -variable- <= -amount of files in folder-
{do command on file -variable-
increase variable by one}

or in a more java like synthax:

count=0;
while (count <= filecount)
{
command
count++
}


I'm just curious how to apply a command in every file in the folder it doesn't have to involve a counter cariable or a while loop for that matter.

Cheers
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 8, 2004, 08:33 PM
 
Originally posted by digiology2:
I'm just curious how to apply a command in every file in the folder it doesn't have to involve a counter cariable or a while loop for that matter.
This is how I do it:

Code:
for file in `ls`; do # do something with $file here done
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:23 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2