How to read in a part of an echo line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read in a part of an echo line?
# 1  
Old 10-31-2012
How to read in a part of an echo line?

Hello,
I have a very basic script
Code:
#!/usr/bin/ksh
      while
print ' '
print '          1. View Command History '
print '          2. List files in current Directory '
 read opt'?Enter Option> ' ;do
if [ $opt = 1 ] ;then
      fc -l
      fi
#     
      if [ $opt = 2 ] ;then
      ls -la

I want to add some more options to do a 'cd' command but without coding all the 'if' statements.
Is there a way to list the options (all the directory names) and then cd to that directory based on the number supplied?

Thanks

Rob

Last edited by Franklin52; 10-31-2012 at 10:44 AM.. Reason: fixed code tags
# 2  
Old 10-31-2012
Quote:
Originally Posted by Grueben

Is there a way to list the options (all the directory names) and then cd to that directory based on the number supplied?
What about using case..?

You can add multiple options..

sample code..

Code:
case "$1" in

1)  echo "Sending 1 signal"    
    ;;
2)  echo  "Sending 2 signal"
    ;;
3)  echo  "Sending 3 signal"
    ;;
*) echo "Signal number $1 is not processed"
   ;;
esac

# 3  
Old 10-31-2012
You also may want to have a look at this link
# 4  
Old 10-31-2012
Not sure if this works the same in ksh, but according to man page you might want to give it a shot:
Code:
allfiles=$(ls)
select selfile in $allfiles
   do echo $selfile
   done

This will return the selected file/directory in the variable selfile, to which you then can cd
# 5  
Old 10-31-2012
You can find some ksh use of "select" here
# 6  
Old 10-31-2012
Thanks I'll have a look but I don't really understand it !
# 7  
Old 10-31-2012
Quote:
Originally Posted by Grueben
Thanks I'll have a look but I don't really understand it !
Please look..

Code:
$ cat test.sh
case "$1" in

1)  echo "Sending 1 signal"
    ;;
2)  echo  "Sending 2 signal"
    ;;
3)  echo  "Sending 3 signal"
    ;;
*) echo "Signal number $1 is not processed"
   ;;
esac

$ sh test.sh 2
Sending 2 signal

$ sh test.sh 1
Sending 1 signal

$ sh test.sh 5
Signal number 5 is not processed

As per your given option you can change what you want to do.
just replace echo with your command.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

3. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

4. Shell Programming and Scripting

Replacing part of the sentence using echo and sed

Hi, Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong. Sample Code : --> export line="VORTEX,abcdef" export... (3 Replies)
Discussion started by: ajithab
3 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

read variable after echo

Hi, i want to create an user-friendly script where you are asked for two numbers. i would like that these two number to be separated with "--" for example, but i can't figure out how to do this. for example read -p "Insert lowest and highest value: " min ; echo -n "-- "; read max so... (3 Replies)
Discussion started by: ezitoc
3 Replies

7. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

8. UNIX for Dummies Questions & Answers

echo appends to a read-only file

echo command can append to a read-only file too. If the file is read-only, with w flag removed, then even echo should not be able to append to the file. But it is possible. Can anybody explain the below behaviour? /root > echo 'sample text' > file /root > cat file sample text /root >... (2 Replies)
Discussion started by: anand_bh
2 Replies

9. Shell Programming and Scripting

While read do, then echo, gives double output

Sorry about the title, but this should be fairly self-explanatory. My script seems to work, except that in the output, below, the first three lines are correct, but why does it print the additional lines 4, 5, & 6? Script: #!/bin/bash while read; do client="$(grep -m 1 Client | awk... (3 Replies)
Discussion started by: MrKen
3 Replies

10. Shell Programming and Scripting

how to read a file to an echo statement

I was searching for an option where i can echo some strings together with the contents of a file. Eg. I was to echo the below string to a file "alter application PMS_ add variable CurrYear Y2009;" >> ${ESS_MAXL_DIR}/PMS_SUB.txt The value 2009 is coming from a file called date.txt without the... (3 Replies)
Discussion started by: Celvin VK
3 Replies
Login or Register to Ask a Question