String extraction from user input - sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String extraction from user input - sh
# 1  
Old 02-19-2005
String extraction from user input - sh

Hi,

I have a shell script to build components of a product. The follow snippet will explain what I am doing.

# !/bin/sh
for choice in "$@" ;
do
case $choice in
"o") echo "Calling $choice" ; o ;;
"i") echo "Calling $choice" ; i ;;
"w") echo "Calling $choice" ; w ;;
"m") echo "Calling $choice" ; m ;;
"all") o ; i ; w ; m ; ftp ;;
*) usage ;;
esac
done


Now when I call the script I call it in the following manner: sh build.sh i o w m

What I plan to do is take the input as a single argument containing the required components; sh build.sh iowm

Is there anyway to extract the individual charaters from the input string. And then check for i/w/o/m and then carry on with the build.

Also is there a way to check for 0 arguments using $@ . Currently I use
if test $# = 0

Thanks in advance,
Vino
# 2  
Old 02-19-2005
What system are you using? Post the output of "uname -a".
# 3  
Old 02-19-2005
Linux staci21 2.4.21-27.ELsmp #1 SMP Wed Dec 1 21:59:02 EST 2004 i686 i686 i386 GNU/Linux
# 4  
Old 02-19-2005
That makes a big difference. Your /bin/sh is actually bash rather than the old bourne shell. Try something like:
Code:
#! /bin/sh

parm=$1

while ((${#parm})) ; do
        rest=${parm#?}
        char1=${parm%$rest}
        parm=$rest
        echo $char1 $parm
done
exit 0

# 5  
Old 02-20-2005
But I have set the sh to /bin/sh. Does it still make a difference ???
# 6  
Old 02-20-2005
Quote:
Originally Posted by vino
But I have set the sh to /bin/sh. Does it still make a difference ???
No, it shouldn't matter - most Linux systems have /bin/sh linked to /bin/bash - to check, do

ls -li /bin/sh /bin/bash

see if a) the inode numbers are the same or b) there's a soft link.

Cheers
ZB
# 7  
Old 02-20-2005
Perderabo,

Thanks for the solution. That extracts the first character. Is there anyway I can search for a character in the input string. For eg. if the input string is iowm, I need to search for the character o in the string. Something like grep on a charatcer string rather than a file.

Vino
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. Shell Programming and Scripting

String Extraction

I am trying to extract a time from the below string in perl but not able to get the time properly I just want to extract the time from the above line I am using the below syntax x=~ /(.*) (\d+)\:(\d+)\:(\d+),(.*)\.com/ $time = $2 . ':' . $3 . ':' . $4; print $time Can... (1 Reply)
Discussion started by: karan8810
1 Replies

3. Homework & Coursework Questions

Function to Check if string input from user is alphabetic only

Good Evening. I'm new to C. Can you please help me. I'm creating an error checking function, user will input a string, this will check if the input is all alphabet or all letters only. If there is a digit or other special char, it will print Error then ask input from user again. Here's my... (1 Reply)
Discussion started by: eracav
1 Replies

4. Shell Programming and Scripting

String generation from user input

Hi I have one thing I need advice on, and I don't know where to start so I have no sample code. I want the user to provide input like: 1-3,6,7,9-11 When the input is like this, I want a string to be generated including all the numbers. In the example above, the string would look like: 1... (13 Replies)
Discussion started by: Tobbev
13 Replies

5. UNIX for Dummies Questions & Answers

Delete a line containing a string from user input

I have code that accepts input from a user, and when the user hits enter it is supposed to delete that whole line from the file. echo "Which record? " read record sed '/$record/d' file However, it does not delete it. Any help? (1 Reply)
Discussion started by: itech4814
1 Replies

6. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

7. UNIX for Dummies Questions & Answers

Extraction of numbers from input

I have the following input: xxxx-2.7.19_WR3 I have extract the following: 2.7.19 Can anyone suggest an awk or sed command to do the above...:confused: another condition is if the input is : xxx-xxx_xxx-1.4-1_WR3.0bg.armv6jel_vfp it shud still extract : 1.4-1 whenever "-"... (12 Replies)
Discussion started by: xerox
12 Replies

8. Shell Programming and Scripting

Sub-string extraction on arrays

Hi, I'm trying to extract the middle of an array that is of variable length but always has a first and last common element, The following works OK... #!/bin/bash ARRAY='switch' ARRAY='option1' ARRAY='option2' ARRAY='option3' ARRAY='value' SWITCH=${ARRAY:0:1} VALUE=${ARRAY:(-1)}... (1 Reply)
Discussion started by: ASGR
1 Replies

9. Shell Programming and Scripting

String Extraction in Perl

I have a string stored in a variable. For instance, $str = " Opcode called is : CM_OP_xxx " where xxx changes dynamically and can be either LOGIN or SEARCH..... depends on runtime. For example : $str = " Opcode called is : CM_OP_SEARCH " $str = " Opcode called is : CM_OP_LOGIN " I... (3 Replies)
Discussion started by: vkca
3 Replies

10. Shell Programming and Scripting

Extraction of the output from a string.

Hi Everyone, I stored the result of a certain awk script in the variable arr.The result is /inets /banking /tools. arr= /inets /banking /tools These are 3 direcctories. I should be able to move in to these directories using "cd" command.Can you tell me how to extract... (5 Replies)
Discussion started by: saicharantej
5 Replies
Login or Register to Ask a Question