shell programming if statement help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell programming if statement help
# 1  
Old 03-03-2012
shell programming if statement help

Hello

i am trying to change every word that starts with "a" or "b" . if it starts with one of the letters i want to add xx rho the end of the word.

Example : boy ==> boyxx

that what i did so far

PHP Code:
#!/bin/ksh
echo "please enter a word: "
read word
if[ $word -eq a ] || [ $word -eq b ]; then
echo "the word is $wordxx"
else 
echo 
"the word is $word"
fi 
thank you
# 2  
Old 03-03-2012
If "boy" is entered it will never equal 'a' or 'b' and so the expression will always evaluate to false.

Since you are using kshell, the right hand side of an expression in double brackets is treated as a pattern and you can code:

Code:
[[ $word == "b"* ]]

to test if the expansion of $word is a 'b' followed by zero or more characters.

The asterisk (*) must be outside of the quotes or shell will not treat it as a meta character.

If you must, or would rather, use single brackets, then you can code something like this:

Code:
[ "${word:0:1}" -eq "b" ]

Which will take the first character of the value of word and compare it to 'b.' If you try to use pattern characters inside of single brackets the shell will treat them as filename globbing characters and you'll end of with undesired results.
# 3  
Old 03-03-2012
thank you

how can i add the "cc" to the end of the word
# 4  
Old 03-03-2012
Ha! I missed that!

Variable expansion syntax is generally $varname, however when you need to append something to the expanded content, and adding that would form another legitimate variable name ($varnamexx for example), you need to use one of two variations to cause shell to see $varname correctly.

The preferred way putting curly braces round the variable name like either of these:
Code:
${varname}xx
${varname}"xx"

However, this will usually work too:
Code:
$varname"xx"

If what is being appended is not a legitimate variable name character (a dot forinstance) then you can code something like:

Code:
$varname.jpg

As the shell will stop parsing and recognise the end of the variable name when it encounters a non-name character.

Last edited by agama; 03-03-2012 at 09:24 PM.. Reason: clarification
# 5  
Old 03-03-2012
MySQL

Thank you agama very helpful

# 6  
Old 03-04-2012
b=are
[[ $b =~ ^b ]] && echo match
# 7  
Old 03-04-2012
Any shell:
Code:
case $word in 
  [ab]*) echo "the word is ${word}xx";;
      *) echo "the word is $word";;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

Shell programming

Hi every one,i am new to unix.Can any one tell me about shell programming.. (1 Reply)
Discussion started by: martina100011
1 Replies

3. Shell Programming and Scripting

Shell Programming

Files to be merged. File1(1.txt) ----------- 1|2|Y 2|5|N File2(2.txt) ----------- 1|abc 2|xyz 3|pqr My Code: --------- nawk ' BEGIN { FS=OFS="|" } NR==FNR { (1 Reply)
Discussion started by: greenworld
1 Replies

4. Shell Programming and Scripting

need help for shell programming

My purpose was to print out all of name of students in a list.First of all,I created a file name "List" in /home/tuan/Desktop/Shell_programming as below Tom Henry Ben Linda Marry And my script "script" is #!/bin/sh path=/home/tuan/Desktop/Shell_programming/List for student in $path... (3 Replies)
Discussion started by: thungmail
3 Replies

5. Shell Programming and Scripting

shell programming

Hi iam new to shell programming. I would like to ask one dought abt the file handling in unix. Iam having a file1 as follows: ASDERFCX1234567890123456 POIUYTRE0098765432123456 BVCXCVBN0955644411111111 File2 ASDERFCX1234567890123456 kill@abc.com ... (8 Replies)
Discussion started by: nivas
8 Replies

6. Shell Programming and Scripting

Shell programming

Hi, Iam new to shell program, I want to check a file which is having same lines 2 times and i want to display it in a seperate file. File format is : AQWERTYU|1234567890 ASDFGHJK|0987654321 ZXCVBNML|1098576453 AQWERTYU|1234567890 I need to take the 1st and 4th lines in the above... (5 Replies)
Discussion started by: nivas
5 Replies

7. Shell Programming and Scripting

{} in shell programming

Could someone please tell me what {} mean when they surround a variable? For instance, $FILE = 'basename $1' //what is passed into this script $BANK = 'dirname $1' $INFILE = ${FILE}.${BANK}.$$ What does $INFILE contain after this assignment? Please let me know Thanks G (4 Replies)
Discussion started by: vgirijanaidu
4 Replies

8. Shell Programming and Scripting

Shell Programming

Provides a menu structure that allows a user to select several options - input a name and address, lookup a name and address, delete a name and address, and print a formatted report from a text database (note: this requirement assumes that you will use the program to create a database, though in... (1 Reply)
Discussion started by: DonOmar
1 Replies

9. Shell Programming and Scripting

Shell Programming?

For what purposes should we use shell /what are the tasks we can achieve using shell which is best book to learn shell programming and will nayone tell me diff between shell programming aand shell scripting? Thank u in advance. (1 Reply)
Discussion started by: shrikrishna
1 Replies

10. UNIX for Dummies Questions & Answers

Shell Programming

I have a fix_table.ksh script that takes a TABLENAME and a date. So, in jk_table_file.txt I have the tables...one per line, and in jk_out_file.txt I have the date in the format I need. The following doesn not 'want' to work in a shell script... for TABLE in `cat jk_table_file.txt`; do ... (2 Replies)
Discussion started by: JWK1
2 Replies
Login or Register to Ask a Question