Am I abusing backticks?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Am I abusing backticks?
# 1  
Old 06-05-2009
Am I abusing backticks?

I'm always concerned I might be abusing backticks within my scripts. A current script I'm writing has this for example:

stripscriptname=`echo $scriptname | sed 's/\(.*\)\..*/\1/'`
stripsearch=`echo $searchpattern | tr -d ' ,/'`

Both of these variables are set inside the script (in fact, $scriptname is set with "scriptname=`basename $0`") so it seems to me that I shouldn't need to use backticks but I don't know a better way to do this.

Am I abusing backticks? Is there a better way to accomplish what I did in the above examples?

MG
# 2  
Old 06-05-2009
I might write:
Code:
stripscriptname=$(sed 's/\(.*\)\..*/\1/' <<< "$scriptname")
stripsearch=$(tr -d ' ,/' <<< "$searchpattern")

I think avoiding the echo cuts out one extra process, builtin or not.

Is the purpose of your sed search string to strip off any string beginning with dot from the end of the string? It might work faster(and more portably, since it needs no backreferences) to just match and delete the portion you don't want without matching the part you want to keep:
Code:
sed 's/\..*//'

Some shells, like bash 3 and newer, support built-in regular expressions without resort to sed.
# 3  
Old 06-05-2009
Quote:
Originally Posted by mglenney
Is there a better way to accomplish what I did in the above examples?

Use parameter expansion; it's many times faster than calling an external command. For example, instead of

Code:
scriptname=`basename $0`

Use:

Code:
scriptname=${0##*/}

To remove an extension:

Code:
stripscriptname=${scriptname%.*}

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nesting backticks

I'm trying to make a dialog window that prints the output of grep that takes the output of find. Unfortunately my nested backticks don't work. Here is the dialog window: dialog --stdout --title "test" --backtitle "test" --msgbox "Test:\n `grep -l "${tablica}" `find $string``" 16 60I think I... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

2. Forum Support Area for Unregistered Users & Account Problems

Trouble Registering? Countries or Regions Abusing Forums

The forums have been seeing a sharp increase in spam bots, forum robots, and malicious registrations from certain countries. If you have been directed to this thread due to a "No Permission Error" when trying to register please post in this thread and request permission to register, including... (1 Reply)
Discussion started by: Neo
1 Replies

3. Shell Programming and Scripting

SSH and Backticks [solved]

I have been testing a new script and cannot figure out why my `cat spath` will not execute on the remote machine? sudo ssh -p 22344 -o "PasswordAuthentication no" -o "HostbasedAuthentication yes" -l testuser 192.168.1.6 "find `cat spath` -depth" cat: spath: No such file or directory but... (0 Replies)
Discussion started by: metallica1973
0 Replies

4. Shell Programming and Scripting

Help with remove backticks in a text file

Input file: 'data_1' 'data_10' 'data1311' '235data_13' Desired output: data_1 data_10 data1311 235data_13 Can I know how to remove back tick"'" in a file? Many thanks for advice. (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Red Hat

perl backticks: can't redirect output.

Hi everyone. This is a bit of a perl/linux mixed question. I am trying to redirect STDOUT of chsh by using the following line of perl code. system ("chsh -s /sbin/nologin $testing 1>/dev/null"); This should redirect STDOUT to /dev/null but it won't do that for some odd reason. Any ideas or... (6 Replies)
Discussion started by: austinharris43
6 Replies

6. Shell Programming and Scripting

Difference between using xargs and backticks

Hey all. Just a fast question, what is the technical difference between using back ticks and using xargs to perform a command? Here's an example Find /mydir -name *.conf |xargs rm Vs Rm 'find /mydir -name *.conf' Is there a performance hit? I know they do the same thing but which is... (1 Reply)
Discussion started by: msarro
1 Replies

7. Shell Programming and Scripting

Backticks within backticks?

Hi, I'm trying to do something like this: range= `expr `date '+%m'` - 1` and it does not work. How can I tell it to evaluate an expression within another expression evaluation? I was at first worried that `date '+%m'` would return a string but apparently expr does the math okay normally, so the... (3 Replies)
Discussion started by: jeriryan87
3 Replies

8. Shell Programming and Scripting

Perl - backticks v system in if statements

Can someone explain the difference between backticks and system when evaluated in these if statements: sub getDate { print "start date\n"; if ( system("/bin/date") ) { print "can't get date\n"; exit(2); } print "finish date\n"; } Returns the following: start date Thu... (5 Replies)
Discussion started by: gjkeenan
5 Replies
Login or Register to Ask a Question