LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-22-2004, 02:21 AM   #1
Leo Simon
LQ Newbie
 
Registered: Aug 2004
Posts: 22

Rep: Reputation: 0
Emulating :r and :e within a /bin/sh script


I'd like to write the following kind of script in sh, using the expression :r from tcsh.

#!/bin/sh
file = ${1}:r

This would convert an argument with an extension to just the root of the extension.

But sh doesn't support :r, :e, etc. Is there another way to accomplish this/

Thanks very much for your help
 
Old 10-22-2004, 02:47 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Code:
#!/bin/bash
file=${1/.*/}
echo $file

#Or with environment variables
foo=bar.baz
echo ${foo/.*/}

#.. or with sed
f=`echo $foo | sed 's/\..*//g'`
echo $f

#.. or with awk
f=`echo $foo | awk -F. '{print $1}'`
echo $f

#or with perl
f=`echo $foo | perl -ne 'split /\./; print $_[0];'`
echo $f

#with python
f=`python -c 'import sys; print sys.argv[1].split(".")[0]' $foo`
echo $f
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bin/cue emulating mantasman Linux - Software 1 05-27-2005 11:35 PM
bash script, line1 #!/bin/sh - what does it mean? ombill Linux - Newbie 5 07-05-2004 11:38 AM
A Recycle Bin script. 95se Linux - Software 2 12-19-2003 11:12 AM
/bin/sh script question sancho5 Linux - General 2 12-29-2001 11:12 PM
Shell script does not recognize #!/bin/sh mmvaldez Programming 5 10-05-2001 09:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:50 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