Pressing Enter (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pressing Enter (bash)
# 22  
Old 02-09-2009
Quote:
Originally Posted by Yakuzan
Since I'm in computer science I asked my teacher, he showed me the basename method, worked like a charm.

Thanks for the
echo "${PWD##*/}"

Will try understanding what does ##*/ mean.
This will take the value in the variable ( ${VAR} ) and find the longest pattern that matches the pattern after ## and remove it.

This value in ${VAR} was /abc1/abc2/abc3 and the pattern given to match was "*/". This matched "/abc1/abc2/" as the longest matching pattern from the start of the variable value and removed it leaving only "abc3".

You can get the shortest match from the start by using only 1 # as in ( ${VAR#*/} )

Code:
[user#host ~]$ VAR=/abc1/abc2/abc3
[user#host ~]$ echo ${VAR##*/}
abc3

[user#host ~]$ echo ${VAR#*/}
abc1/abc2/abc3

The shortest match was "/" so the result was that the beginning of the variable now starts with "abc1" and not "/abc1".

You can match from the end of the variable by using '%' and '%%' in the same manner.
Code:
[user#host ~]$ VAR=/abc1/abc2/abc3
[user#host ~]$ echo ${VAR%/*}
/abc1/abc2

[user#host ~]$ echo ${VAR%%/abc2*}
/abc1




Taken from man page for bash:

${parameter##word}
The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches
the beginning of the value of parameter, then the result of the expansion is the expanded value of
parameter with the shortest matching pattern (the ‘‘#'' case) or the longest matching pattern (the
‘‘##'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each
positional parameter in turn, and the expansion is the resultant list. If parameter is an array
variable subscripted with @ or *, the pattern removal operation is applied to each member of the
array in turn, and the expansion is the resultant list.
# 23  
Old 02-10-2009
I'm trying to create a script that places you in any directory that you input the name.(No need for the whole path)

I've finished doing it but I'm now experiencing a problem because I'm trying to echo: "Directory doesn't exist" using an if condition but it's not working.

When I write tftftft as a folder name the find command gives an explicit message that the directory doesn't exist but my if statement(That contains an echo) isn't popping out.

Here's the test part:

Code:
...
if [ ! -e $variableWithPathInside ]
then 
echo "Directory isn't existent"
else
cd $variableWithPathInside
...
fi

So why isn't my test working?

Actually I'm an idiot.

But how could I express that if the find doesn't find anything it shows up a message?

read -p "Name of directory" $rep
if [ -d find -name $rep ] .....?

Last edited by Yakuzan; 02-10-2009 at 11:59 AM..
# 24  
Old 02-10-2009
i'm going to guess that since the directory doesn't exist, $variableWithPathInside is empty. so you're test to see if <file> exists is testing nothing and returning a success. bash (and other shell scripting languages) is different in that you don't need to declare variables. calling to a previously undeclared/undefined variable just returns "", just like it's doing in your script. it's going to the else block and executing a cd command with no parameters, which just changes directories to the users home: defined in env as $HOME.

instead, try something like this:
Code:
# $variableWithPathInside is already defined by user
 
dirLoc=`find <startLocation> -type d -name $variableWithPathInside`
 
if [ -z $dirLoc ] #tests to see if variable has zero length.  can use -n for nonzero length
then
    echo "Directory isn't existent"
else
    cd $dirLoc
fi

give that a go
# 25  
Old 02-10-2009
If I understand you correctly:
Code:
DIR=""
while [[ -z "${DIR}" || ! -d "${DIR}" ]]
do
        read -p "Enter dir: " DIR
done

The double-brackets are necessary for the || construct inside. -d is preferred to -e when checking for directories, since -d will only succeed for directories, while -e will accept any kind of file at all.
# 26  
Old 02-10-2009
Try this instead:

Code:
read -p "Enter dir: " DIR
if [ ! -d "$DIR" ]
then 
     echo "Directory $DIR dosn't exist"
else
     cd $DIR
     ...
fi

# 27  
Old 02-10-2009
that's not going to work. he said he doesn't want the user to have to enter the entire path. he just wants to enter a directory and the use the find command to find it...when using the find command with the -type d argument, it's already testing for it to be a directory...no need to test for it again.
# 28  
Old 02-10-2009
sorry missed that Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script termination without pressing Enter key[nohup]

I have a script in which we have used nohup. Once script is executed it will be terminated only when enter key is pressed. I want the script to be terminated without pressing enter key nohup imqbrokerd -name user_id port 2>1 1>$home_`date` & I am a newbie to shell, Kindly please help (3 Replies)
Discussion started by: Suganbabu
3 Replies

2. Shell Programming and Scripting

Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed. #!/usr/bin/expect -f... (2 Replies)
Discussion started by: jbrass
2 Replies

3. Shell Programming and Scripting

How to pass enter key or selected character in bash script?

hi, i've bash script thats working... but now i need to add a line....that prompts for user input....like yes and 1 as complete install.... so here's how it looks... $ cd 9200 (cd into directory) $./install (hv to type ./install to run install then ask for) ----do you want to... (4 Replies)
Discussion started by: kernel11
4 Replies

4. Shell Programming and Scripting

Perl - start search by using search button or by pressing the enter key

#Build label and text box $main->Label( -text => "Input string below:" )->pack(); $main->Entry( -textvariable => \$text456 )->pack(); $main->Button( -text => "Search", -command => sub { errchk ($text456) ... (4 Replies)
Discussion started by: popeye
4 Replies

5. Shell Programming and Scripting

Want to write a command that keeps pressing enter key in regular interval

Hi, I m new to linux, can you pls help me to write a command/ script that keeps pressing the enter key in regular interval. Thx, Linux1 (2 Replies)
Discussion started by: mylinux1
2 Replies

6. Shell Programming and Scripting

Pressing enter and script still goes on

i have a bash shell script and i press enter and the script still continues on? how do i stop this (3 Replies)
Discussion started by: gangsta
3 Replies

7. Shell Programming and Scripting

How to enter a return key in bash script?

Hi, I'm porting an install script from AIX to Red Hat (2.6.18-164.el5 #1 SMP) I have this script working in both AIX and HP-UX. The script is a wrapper for a Micro Focus Server Express install program. It responds to the install program questions with a here-now list. Responses includes... (14 Replies)
Discussion started by: duker61
14 Replies

8. Shell Programming and Scripting

Pressing "Enter/Space bar" using Net::TELNET? in Perl

I'm trying to learn how to get my script to execute the enter button when it telnets into a router and the router displays output but you need to press the space bar or enter button to continue displaying my output of the router. How is this done? (0 Replies)
Discussion started by: xmaverick
0 Replies

9. Shell Programming and Scripting

BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?

I'm working on making a menu system on an HP-UX box with Bash on it. The old menu system presents the users with a standard text menu with numbers to make selections. I'm re-working the system and I would like to provide something more akin to iterative search in Emacs. I have a list of 28... (2 Replies)
Discussion started by: deckard
2 Replies

10. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies
Login or Register to Ask a Question