LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-25-2012, 03:36 AM   #16
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133

Aagam it looks like there's something funny going on with your shell. What's the output of these commands?

Code:
bash --version

ls -l /bin/sh
Did you try running these scripts as a different user? From a different terminal emulator, or a regular tty? Or maybe reinstalling bash? If nothing works, try running the scripts using a Live CD.

Quote:
and plz tell me the command used to extract a letter from the string by specifying its position...
let's say that

Code:
name=Aagam
To print the first (position zero) letter (A):

Code:
echo ${name:0:1}
So, you give the name of the variable, the position starting from zero, and the number of letters.
 
1 members found this post helpful.
Old 01-25-2012, 03:56 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Nothing funny with the shell but rather how the OP is running all of our scripts.

By calling the scripts like so:
Code:
sh namesum
You are overriding the fact that we are using bash and you are now using sh.

Please advise if you require your script to be a POSIX sh script? (many of the features used by myself and others are bash)

If not, simply call each script like so:
Code:
./namesum
You will of course need to make it executable
 
1 members found this post helpful.
Old 01-25-2012, 04:08 AM   #18
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
@grail
Maybe, although 'sh namesum' works fine on my mac (and /bin/sh is not just a link to bash). But you're right on that he should run the scripts the proper way.
 
Old 01-31-2012, 11:15 AM   #19
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
hiiii everyone.....sry for late reply....
i ve tried this script, plz correct me where i am wrong
Code:
read -p "Enter your name : " name
a=[a-z]
n=${#name}
i=0
while [ $i -lt $n ]
do
m=${name:$i:1}
n="index $a '[1-26]*.$m'"
echo $n
done
i=`expr $i + 1`
i knw that it doesnt give sum of the numbers but i just tried to see if intermidiate commands are working,...
and they are not....
theres some error in
Code:
m=${name:$i:1}
as it give output BAD SUBSTITUTION err
 
Old 01-31-2012, 11:25 AM   #20
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
actually i didnt read last two replies properly, i get it now.....grail is right......each of those scripts works..........thnx everybody there.
n plz also correct my script
 
Old 01-31-2012, 12:22 PM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Try using set -xv at the start of your script as it may point to your error.
 
Old 02-01-2012, 11:45 AM   #22
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by Aagam View Post
Code:
read -p "Enter your name : " name
a=[a-z]
n=${#name}
i=0
while [ $i -lt $n ]
do
  m=${name:$i:1}
  n="index $a '[1-26]*.$m'"
  echo $n
done
i=`expr $i + 1`
Hi,

two things are wrong with your script:
- you are incrementing $i outside the loop
- you are assigning a string to $n; therefore the test [ $i -lt $n ] fails

Not sure what output you expected but this gives no error:
Code:
read -p "Enter your name : " name
a=[a-z]
n=${#name}
i=0
while [ $i -lt $n ]
do
  m=${name:$i:1}
  echo "index $a '[1-26]*.$m'"
  echo $n
  i=`expr $i + 1`
done
Hope this helps.
 
Old 02-07-2012, 09:59 AM   #23
Aagam
LQ Newbie
 
Registered: Dec 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
@crts- yup i got it....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 08:51 PM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 04:39 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 07:36 AM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 10:23 AM


All times are GMT -5. The time now is 08:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration