Pressing Enter (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pressing Enter (bash)
# 15  
Old 02-09-2009
Quote:
An exernal command where none is needed.
oops...

I guess we are assuming echo is built-in and not /bin/echo (GNU external).

Code:
[user@host ~]$ echo --version
--version
[user@host ~]$ /bin/echo --version
echo (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

# 16  
Old 02-09-2009
Quote:
Originally Posted by ddreggors
I guess we are assuming echo is built-in and not /bin/echo (GNU external).

It has been built into every shell for probably about 20 years. POSIX requires that such commands also be available as external commands.

printf, which is recommended over echo, is built into all major POSIX shells.
# 17  
Old 02-09-2009
I realize it is built-in and has been for a while. I just get so used to using complete paths when writing scripts that I overlook the built-ins from time to time.

I have seen more than a few scripts fail because the ${PATH} var is not set as expected when running in a cron or run remotely.

A quick way around that is to use full paths for all commands and when you get used to that technique in a script... built-in commands get used far less then their external counterparts.

Another thing that I notice is that the built-ins behave differently than expect in some cases since they are usually not the full replica of their external counterparts.

Take for example my previously posted echo example with the --version flag.


Either way, I agree that more efficient is better and certainly your example was shorter and has less commands than mine.

I was just trying to point out that the parameter expansion constructs are not always the easiest to remember and that their ARE alternatives. Some are far easier to remember as well. Efficient is also from the view point of the person writing the script. How efficient is it when you have to go read man pages or google and answer and you spend 20-30 extra minutes writing a single line because you cannot remember the exact method for the parameter expansion you want to use?
# 18  
Old 02-09-2009
Quote:
Originally Posted by ddreggors
I was just trying to point out that the parameter expansion constructs are not always the easiest to remember and that their ARE alternatives.
Absolutely. String operations can also be difficult to apply to a given problem when there isn't one to do exactly what you want. In any case, it's useful to have a list of them. Since bash 3, it supports built-in regular expressions too...
Quote:
Some are far easier to remember as well. Efficient is also from the view point of the person writing the script.
I'll have to disagree with you on that one. In lower-level languages like C, the easier way is often better because its clearer and simpler and not that much slower in any case. But the cost of launching a process is enormous, you should use processes for:
  • things you positively cannot do in bash, i.e. network operations
  • things where the process does a lot of work, i.e. tar/gzip
Launching lots of short-lived processes for tiny things can easily magnify the running time of your script to the point where it takes huge amounts of time to do anything non-trivial.

Last edited by Corona688; 02-09-2009 at 12:32 PM..
# 19  
Old 02-09-2009
Quote:
Originally Posted by ddreggors
I realize it is built-in and has been for a while.

More than "a while": for the entire life of the Bourne shell and its successors.
Quote:
I just get so used to using complete paths when writing scripts that I overlook the built-ins from time to time.

I have seen more than a few scripts fail because the ${PATH} var is not set as expected when running in a cron or run remotely.

I have seen more than a few scripts fail because the full path was used. It's far easier to adjust the PATH, if necessary, than to change all the commands when running a script on a different system.
Quote:

A quick way around that is to use full paths for all commands and when you get used to that technique in a script... built-in commands get used far less then their external counterparts.

In an efficient script, it is quite the opposite. Many of my scripts call no more than one external command, and many call none.
Quote:

Another thing that I notice is that the built-ins behave differently than expect in some cases since they are usually not the full replica of their external counterparts.

Take for example my previously posted echo example with the --version flag.

I haven't seen that.

The use of echo is deprecated, and it should not be used when the contents of its arguments are not known in advance.
Quote:

Either way, I agree that more efficient is better and certainly your example was shorter and has less commands than mine.

I was just trying to point out that the parameter expansion constructs are not always the easiest to remember and that their ARE alternatives. Some are far easier to remember as well. Efficient is also from the view point of the person writing the script. How efficient is it when you have to go read man pages or google and answer and you spend 20-30 extra minutes writing a single line because you cannot remember the exact method for the parameter expansion you want to use?

20-30 minutes to do: "man bash / Parameter Expansion <ENTER>"???
You must be joking!!

You might want to use my sman function (see below).

Besides, after using them a few times, you will not have to look them up, and your scripts will be much faster. Learn them now before bash4 is released; it has many more parameter expansions (some of which I suggested).

Code:
sman() ##display a man page with a search 
{
    LESS="$LESS${2:+ +/$2}" man "$1"
}

# 20  
Old 02-09-2009
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.
# 21  
Old 02-09-2009
Please don't misunderstand me, I agree with you on the points you make. I am not disputing these points. I am merely stating that for a new user, some of the conventions you use here are daunting to say the least. Some are also quite simple and elegant but to the beginner something like "${VAR%/*}" is greek Smilie

That said, yes... your scripts and you skill level will improve the more you use better scripting habits. Never has been a question there either.

I would suggest to any that wish to learn that they take all examples you and others give and learn the differences as they get more comfortable.

The 30 minute reference was meant to refer to time spent doing google searches. A lot of beginners neglect man pages. Either from lack of knowledge of their existence or from lack of good habbit.


Hey nice function (sman) by the way.
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