Setting letter as variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting letter as variable
# 1  
Old 09-27-2012
Setting letter as variable

I need to pass letter like "c" for "-copy" and need to read by variable $sel_mod, My I know the good way of writing the script.

Example:

Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy')
Type the Clone mode : c

Thanks in advance,
Ashan

Code:
set_env1(){
clear
echo
echo
echo
echo
echo "      SETUP CLONE ENVIRONMENT "
echo
echo -e "\e[1;31m      Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy' )\e[0m"
echo
echo -n "      Type the Clone mode           :"
read sel_mod
echo
echo -n "      Enter Array I.d (-sid)        :"
read sid
echo
echo -n "      Enter Clone Device Pair File  :"
read dev1
echo
echo
echo "    -------------------------------------------"
echo
echo -e "\e[1;34m      Entered Clone Mode is  : (*** $sel_mod ***) \e[0m"
echo
echo -e "\e[1;34m      Entered Array Id is    : (*** $sid ***) \e[0m"
echo
echo -e "\e[1;34m      Entered Clone File is  : (*** $dev1 ***) \e[0m"
echo
echo
echo "    -------------------------------------------"
echo
echo -e "\e[1;31m      Continue if entries are correct or go back to menu reselect.....!!! \e[0m"
echo

MOD=$sel_mod
ID=$sid
DFILE1=$dev1
DFILE2=$dev2
}
set_env1


Last edited by Corona688; 09-27-2012 at 06:56 PM..
# 2  
Old 09-27-2012
Maybe this:

Code:
 
get_opt(){
OPTS=":-copy:-differential:-precopy:-nopcopy"
opt="${1#[-]*}${OPTS##*:-$1}"
opt="-${opt%%:*}"
opt="-${opt#[-]*}"
echo $opt
}
set_env1(){
clear
echo
echo
echo
echo
echo " SETUP CLONE ENVIRONMENT "
echo
echo -e "\e[1;31m Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy' )\e[0m"
echo
echo -n " Type the Clone mode :"
read sel_mod
sel_mod=$(get_opt $sel_mod)
echo
echo -n " Enter Array I.d (-sid) :"
read sid
sid=$(get_opt $sid)
echo
echo -n " Enter Clone Device Pair File :"
read dev1
dev1=$(get_opt $dev1)
echo
echo
echo " -------------------------------------------"
echo
echo -e "\e[1;34m Entered Clone Mode is : (*** $sel_mod ***) \e[0m"
echo
echo -e "\e[1;34m Entered Array Id is : (*** $sid ***) \e[0m"
echo
echo -e "\e[1;34m Entered Clone File is : (*** $dev1 ***) \e[0m"
echo
echo
echo " -------------------------------------------"
echo
echo -e "\e[1;31m Continue if entries are correct or go back to menu reselect.....!!! \e[0m"
echo
MOD=$sel_mod
ID=$sid
DFILE1=$dev1
DFILE2=$dev2
}
set_env1

# 3  
Old 09-27-2012
Thanks for your reply can you please explain the code and who it work.
Code:
get_opt(){
OPTS=":-copy:-differential:-precopy:-nopcopy"
opt="${1#[-]*}${OPTS##*:-$1}"
opt="-${opt%%:*}"
opt="-${opt#[-]*}"
echo $opt
}


Last edited by Scrutinizer; 09-27-2012 at 10:32 PM.. Reason: code tags
# 4  
Old 09-27-2012
Code:
get_opt(){                                                   # define get_opt function
OPTS=":-copy:-differential:-precopy:-nopcopy"  # set the OPTS string variable
opt="${1#[-]*}${OPTS##*:-$1}"                    # find the matching string in OPTS using the $1 variable to the function
opt="-${opt%%:*}"                                       # remove the ":" to the end of string after the matching string
opt="-${opt#[-]*}"                                        # force "-" at beginning of opt string
echo $opt                                                     # return opt string
}

# 5  
Old 09-27-2012
Issue is rest of the output also get hyphen mark, need to avoid hyphen shown below.


Code:
Entered Array Id is : (*** -4122 ***)
 
 Entered Clone File is : (*** -dev.txt ***)
----------------------------------------------------------------------------------------------
SETUP CLONE ENVIRONMENT
 
 Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy' )
 
 Type the Clone mode :c
 
 Enter Array I.d (-sid) :4122
 
 Enter Clone Device Pair File :dev.txt
 
 
 -------------------------------------------
 
 Entered Clone Mode is : (*** -copy ***)
 
 Entered Array Id is : (*** -4122 ***)
 
 Entered Clone File is : (*** -dev.txt ***)
 
 
 -------------------------------------------


Last edited by Scrutinizer; 09-27-2012 at 10:31 PM.. Reason: code tags
# 6  
Old 09-27-2012
to remove hyphen:

replace line in get_opt function:

Code:
opt="-${opt#[-]*}"

with

Code:
opt="${opt#[-]*}"

# 7  
Old 09-28-2012
Read up on getopts too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

3. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

4. Shell Programming and Scripting

HOWTO - change letter case of variable value

Hi I have e.g. VAR1=january and I need to change it into VAR1=January. How to change value of VAR1 variable to always set first character uppercase and other lowercase ? thx for help. (9 Replies)
Discussion started by: presul
9 Replies

5. Shell Programming and Scripting

Set the last 4 letter in the filename as a variable

Hi all, I want to set the last 4 letter in the filename as a variable. For example, i have AB1234.txt file and i need to have last 4 letter as a variable. It should be like ; get last four letter set var = 1234 How can i write this in C shell?? Thanks, zibi (3 Replies)
Discussion started by: zibi
3 Replies

6. Shell Programming and Scripting

Trying to capitalize first letter of every word in Variable

Total Bash noob, have been successful in doing my script by searching and looking at examples, but I need some assitance with this one, just can't figure it out. In the Bash script I am trying to capitalize the first letter of every word in a string, ideally not changing other capitalization. ... (5 Replies)
Discussion started by: randyharris
5 Replies

7. Shell Programming and Scripting

using sed to add letter after variable

Hi All, I need to have a sed command insert the letter i at the end of some lines in a file. Problem is that where I want to add the i is after variables that change in each file eg, tsal314384' tsal315386' tsal317392' I need to have it like this ... (2 Replies)
Discussion started by: outthere_3
2 Replies

8. Shell Programming and Scripting

Variable setting help please

L=0 cat test.sh | while read line do L='expr $1 + 1' echo $L done echo $l >>> the echo $L at the end produces 0 but i actually want it to produce the number of lines - any idea why this is happening? (16 Replies)
Discussion started by: penfold
16 Replies

9. UNIX for Advanced & Expert Users

how to read each letter from file and store it in variable.

Dear friends, i am writing csh script i have one dat file containing following data.like this. 08FD3 03A26 000FA0 FFFF0 BBA0F 00000 00000 from the above file i want to read each letter and store it in one variable. how it is possible. please help (7 Replies)
Discussion started by: rajan_ka1
7 Replies

10. UNIX for Dummies Questions & Answers

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies
Login or Register to Ask a Question