LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2007, 10:59 AM   #1
crash_override_me
Member
 
Registered: Aug 2005
Location: India, New Delhi
Distribution: Debian Etch, Ubuntu
Posts: 342

Rep: Reputation: 30
Scripting query..


hi..

can anyone tell me wats wrong in this script..? & how do i correct it..?

Code:
# matches pattern

a=$1

for i in a..z
do
 if [ $a == i* ]
 then
   echo "String starts with" $i
 fi
done
 
Old 07-22-2007, 11:49 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Don't keep it a secret. What is this script doing for you, and what do you want it to do differently?
 
Old 07-22-2007, 12:07 PM   #3
Guitarist88
Member
 
Registered: Feb 2004
Location: Nz
Posts: 240

Rep: Reputation: 30
Ok well I haven't done bash in a very long time and I've been concentrating on c++ for school lately but I'll try to help you a bit. I'm sure some more experienced person will come a long and finish what I've started or correct me. I'll do my best.

Ok I gather you are taking in the first parameter sent with the script and seeing what lowercase letter its starts with.

To test string equality you have to use a single equals sign with the test command. Try man test to see all the different operators.

And you forgot that derefernce $ sign thing in front of the i. The only thing I'm not quite sure of is how the * wildcard works next to a string like that. I don't have Bash in front of me to test as I'm residing in the forbidden land right now...

so in the end it will look something like this:

Code:
# matches pattern

a=$1

for i in a..z
do
 if [ $a = $i* ]
 then
   echo "String starts with" $i
 fi
done
I hope that helps.

Last edited by Guitarist88; 07-22-2007 at 12:25 PM.
 
Old 07-22-2007, 12:13 PM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by crash_override_me
hi..

can anyone tell me wats wrong in this script..? & how do i correct it..?

Code:
# matches pattern

a=$1

for i in a..z
do
 if [ $a == i* ]
 then
   echo "String starts with" $i
 fi
done
Code:
a="$1"

for i in {a..z}
do
 if [[ "$a" == "$i"* ]]
 then
   echo "String starts with $i"
 fi
done
You can write it just as (assuming bash) to match any character:

Code:
echo "String starts with "${1%${1#?}}"

Last edited by radoulov; 07-22-2007 at 12:18 PM.
 
Old 07-22-2007, 01:54 PM   #5
crash_override_me
Member
 
Registered: Aug 2005
Location: India, New Delhi
Distribution: Debian Etch, Ubuntu
Posts: 342

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by radoulov
Code:
a="$1"

for i in {a..z}
do
 if [[ "$a" == "$i"* ]]
 then
   echo "String starts with $i"
 fi
done
You can write it just as (assuming bash) to match any character:

Code:
echo "String starts with "${1%${1#?}}"
thanx for the help..!

i have 2 questions:

1. why r we using double [[ .. ]]
2. what exactly is the second script doing..?
 
Old 07-22-2007, 02:25 PM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by crash_override_me
thanx for the help..!

i have 2 questions:

1. why r we using double [[ .. ]]
Because you cannot use wild cards (*) with
the test operator ([):

Code:
radoulov@ubuntu:~$ [ "ab" = "a"* ]&& echo OK||echo KO
KO
radoulov@ubuntu:~$ [[ "ab" = "a"* ]]&& echo OK||echo KO
OK
But the [[ ... ]] construct is not standard (as far as I know).


Quote:
2. what exactly is the second script doing..?
It just prints the first character of
the first argument ($1):

Code:
radoulov@ubuntu:~$ set -- abc;echo "${1%${1#?}}"
a
With bash you can write it also as:

Code:
radoulov@ubuntu:~$ set -- abc;echo "${1:0:1}"
a
With zsh:

Code:
ubuntu% set -- abc;echo "${1[1]}"
a
You can also use printf to substring:

Code:
ubuntu% set -- abc;printf "%.1s\n" "$1"
a

Last edited by radoulov; 07-22-2007 at 02:52 PM.
 
Old 07-23-2007, 02:45 AM   #7
crash_override_me
Member
 
Registered: Aug 2005
Location: India, New Delhi
Distribution: Debian Etch, Ubuntu
Posts: 342

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by radoulov
With bash you can write it also as:

Code:
radoulov@ubuntu:~$ set -- abc;echo "${1:0:1}"
a
what does 1:0:1 does..??
 
Old 07-23-2007, 02:51 AM   #8
crash_override_me
Member
 
Registered: Aug 2005
Location: India, New Delhi
Distribution: Debian Etch, Ubuntu
Posts: 342

Original Poster
Rep: Reputation: 30
1 more query..!

something is wrong wid the code below..
error:
line 7: [: : integer expression expected
line 10: [: : integer expression expected
line 13: [: : integer expression expected
line 16: [: : integer expression expected


Code:
# says good mrng, eve, afternoon, or night depending on time.

x=`date|awk '{ print $4 }'|awk -F:'{ print $1 }'`

if [ "$x" -lt 4 ]
then
  echo "good night"
elif [ "$x" -lt 12 ]
then
  echo "good morning"
elif [ "$x" -lt 16 ]
then
  echo "good afternoon"
elif [ "$x" -lt 20 ]
then
  echo "good evening"
fi
 
Old 07-23-2007, 04:20 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by crash_override_me
Code:
x=`date|awk '{ print $4 }'|awk -F:'{ print $1 }'`
that's because you don't have a space in between -F: and '{print $1}'
There is also no need for double awk calls:
Code:
date|awk '{ n=split($4,d,":")
            if ( d[1] < 4 ){ print "Good night" }
	    if ( d[1] < 12 ) { print "Good morning" }
	    if ( d[1] < 16 ) { print "Good Afternoon" }
	    if ( d[1] < 20 ) { print "Good evening" }   
	
}'
or just use
Code:
hr=$(date +%H)
if [ $hr -lt 4 ]
....
...
fi

Last edited by ghostdog74; 07-23-2007 at 04:21 AM.
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Scripting Query sc_3007 Linux - General 4 12-01-2005 02:00 AM
query saurabhparihar Linux - Newbie 2 05-21-2005 02:00 PM
X -query pacman23 Linux - Software 2 07-22-2004 07:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration