Visit Our UNIX and Linux User Community


BASH Shell script - help with read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH Shell script - help with read
# 1  
Old 08-05-2009
BASH Shell script - help with read

Hi all,

I'm sure this is quite a simple problem, but i'm completely new to shell scripting, so bare with me. Having problems with the read command. Here's what I have:

read -rp "Command to execute: " the_command
...
...
echo "$the_command"
...
...
eval "$the_command"

Now, say, for instance the user enters the command to execute as echo "this is a test" - all works fine... the echo shows the correct command and the eval executes the command, echoing "this is a test".

BUT... if the user enters the command as echo "this id a typeo" - they go back to correct the mistake so that it now reads echo "this is a typeo" - now the script errors. The echo shows the correct command, but the eval has a problem. Is it possible that the variable the_command is getting every keystroke (including backspaces) and not just the final string? Can someone please help out a newbie Smilie

Cheers,
Justin
# 2  
Old 08-05-2009
Please copy and paste your script on here with code tags. What you posted works fine on my Solaris machine.
# 3  
Old 08-05-2009
Code:
#!/bin/sh
read -rp "Command to execute: " the_command
echo "$the_command"
eval "$the_command"

Here's what happens:

Quote:
$ ./test.sh
Command to execute: echo "cat"
echo "cat"
cat
but now i enter the command as echo "cat" but realise it's supposed to be echo "ratty" so i use the left cursor and backspace to fix the typeo;

Quote:
$ ./test.sh
Command to execute: echo "ratty"
echo "cat"
**crash**
Eventually the script will be used to enter commands such as:
shake "c:\test\test.#.tiff" -t 1-100

The reason i need this to work is because I anticipate users will drag a file into the terminal to get the filepath, but will need to change the resulting filepath from "c:\test\test.tiff" to "c:\test\test.#.tiff" for the script to execute.

p.s. I should mention that i'm running the script through Cygwin on a Windows machine.
# 4  
Old 08-05-2009
Does the script have to be run by sh? Have you tried running it with ksh or bash?
# 5  
Old 08-05-2009
More generic version. Echo is echo and read is read.
Code:
echo -e "C.. execute:\c"
read the_command
# or read -r  # what you need ?  I think that editionkeys handling in read depends which shell you use. Why to use -r in this case ?
echo "$the_command"
$the_command
eval "$the_command"

# 6  
Old 08-05-2009
Try using backspace instead of the left cursor key.

When I run it on Cygwin, it does not crash. However, it does not process correctly due to the codes generated by the left cursor key and I get the following:

Code:
./test.sh: line 4: $'cat\E[D\E[D\E[Dratty': command not found

# 7  
Old 08-05-2009
Firstly, thanks for the replies, guys.. i appreciate the help.

ilikecows: yes, changed it to #!/bin/bash but makes no difference.

kshji: I need to use the read -r option so that it interprets the "\" in inputted filepaths correctly.

mrtiller: If I just use backspace, you're right, it works.... However, I'm assuming the users of the script will not necessarily use backspace only and might do as I described and use the arrow keys and backspace - in which case I don't want the script to go funny on me.

I think you're right - it's not processing correctly due to the codes generated by the left cursor and going over the top of what was already there... is there any way I can tell it to ignore those keystrokes and just store the final string, no matter how it is created?

Cheers all,
Justin

Previous Thread | Next Thread
Test Your Knowledge in Computers #784
Difficulty: Easy
The firmware for the TRS-80 Model 100 was developed by Steve Jobs.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. UNIX for Dummies Questions & Answers

Read Bash Script

I am very new to all these programming languages and really love Linux but have only begun to dive into bash scripting... I am curious what's going on with this script... #!/bin/bash if ; then # xen grep control_d /proc/xen/capabilities >& /dev/null if ; then # domU -- do not run on... (2 Replies)
Discussion started by: BrianBlaze
2 Replies

4. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

5. Shell Programming and Scripting

Bash script to read file location

I'm writing a bash script that reads a file location from a user, and I'm wondering how to get the script to accept tab to auto complete the directories that are input. (8 Replies)
Discussion started by: Prodiga1
8 Replies

6. Shell Programming and Scripting

read -n1 -r -p "Press..." key / produces error in bash shell script

Hello! Sorry, for my not so perfect english! I want to stop bash shell script execution until any key is pressed. This line in a bash shell script read -n1 -r -p "Press any key to continue..." key produces this error When I run this from the command line usera@lynx:~$ read... (4 Replies)
Discussion started by: linuxinho
4 Replies

7. UNIX for Dummies Questions & Answers

Cygwin bash script and read command

Hello everyone, I am struggling a bit with a batch script that I need to run in cygwin. I work in winXP and I had to write some awk scripts to do some file manipulation, and now I would like to automate the process by just running a batch file so that my colleagues can use it easily. Now, the... (2 Replies)
Discussion started by: Teroc
2 Replies

8. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

9. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

10. UNIX for Dummies Questions & Answers

how to read double consecutive space in filename for bash shell

Hello, Im using cygwin app which we use the bash shell for scripting. Im trying to read a filename with consecutive spaces on the filename ex: filename<space><space>1.txt ls -lrt filename<space><space>1.txt is working but when I pull that from a file and place it into variable. it reads... (1 Reply)
Discussion started by: james_falco
1 Replies

Featured Tech Videos