LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2013, 12:59 PM   #1
ratotopi
Member
 
Registered: Dec 2011
Posts: 114

Rep: Reputation: 6
how to make it work in bash script


Here is the simple bash script:

#!/bin/bash

echo " Please enter the directory you want to check"
read directory

ls -l $directory


Now when user execute this script I want them to use tab key to auto complete the input like we can in the CLI in linux. How do I do that ? Thank you

Last edited by ratotopi; 04-04-2013 at 01:01 PM.
 
Old 04-04-2013, 01:08 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Tab key won't work with read, when you will type the directory name. Instead you can pass the directory name as argument with script, as:
Code:
~$ ./myscript /path/to/directory
#!/bin/bash
ls -l $1
 
Old 04-04-2013, 04:57 PM   #3
ratotopi
Member
 
Registered: Dec 2011
Posts: 114

Original Poster
Rep: Reputation: 6
@shivaa thank you for your input
so if I want to write the bash script for
rsync -avn /bin/test/ root@serverIP:/bin/test/
i wrote the below script but its not working.

#!/bin/bash
rsync -avn $1/ root@$2:$3/


and the command line will be
./myscript /bin/test serverIP /bin/test

what am i missing
 
Old 04-04-2013, 06:13 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. did that give you an error; if so what exactly?

2. use code tags please
https://www.linuxquestions.org/quest...do=bbcode#code

3. when specifying cmd line args, don't drop trailing /
Code:
# call like this
 ./myscript /bin/test/ serverIP /bin/test/

# code
# use {} around embedded vars to help parser pick them out
#!/bin/bash
rsync -avn $1 root@${2}:$3

# to see what the parser is doing, add set -xv after bash line
#!/bin/bash
set -xv
rsync -avn $1 root@${2}:$3
Read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 04-05-2013, 12:09 PM   #5
ratotopi
Member
 
Registered: Dec 2011
Posts: 114

Original Poster
Rep: Reputation: 6
thank you, I got the result I want without using read

#!/bin/bash
rsync -avn ${1}/ ${2}:${3}/
 
Old 04-06-2013, 03:58 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

You've have almost a hundred posts here. I think you should know this by now.


Now to answer the opening question: In bash, use the -e option in read to tell it to use the readline library. This will give it the full range of editing features, including tab completion.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
[SOLVED] Need help to make script work, combined bash & python Mr. Alex Programming 3 09-01-2012 01:57 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
how to make my code to work on bsd (pptp script on bash) jabka *BSD 1 05-01-2007 12:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration