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#*/} )
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.
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.
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:
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 ] .....?
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.
If I understand you correctly:
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.
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.
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)
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)
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)
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)
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)
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)