[ale] Determining a scripts language?

Alex Carver agcarver+ale at acarver.net
Wed Apr 6 20:08:16 EDT 2016


No echos, other commands or subshells here, use bash built-in regex:

(Make a subdirectory to accept the destination files to make things
cleaner or put the sources in a subdir and move the destinations up a level)

#!/bin/bash
subdir=subdirname
for f in * (or *.* or whatever pattern you want)
do
	cp $f ${subdir}/${f%.*}.txt
done




%.* trims everything from f after the last dot (including the dot)
returning the prefix
%%.* is greedier and will trim everything after the first dot


On 2016-04-06 15:39, Pete Hardie wrote:
> perhaps something lile this:
> 
> for file in *.*
> do
>      parts = $(echo $file| tr "." " ")
>      bname = `basename $parts[0] $parts[1]`
>      cp $file ${bname}.txt #or whatever processing you are doing here with $file 
> the whole name and $base the name sans suffix
> done
> 
> 
> 
> On Wed, Apr 6, 2016 at 6:27 PM, Pete Hardie <pete.hardie at gmail.com 
> <mailto:pete.hardie at gmail.com>> wrote:
> 
>     Is the issue that you don't want a separate command for each suffix, or that
>     you will not know all the suffixes ahead of time?
> 
>     On Wed, Apr 6, 2016 at 6:22 PM, Scott M. Jones <eff at dragoncon.org
>     <mailto:eff at dragoncon.org>> wrote:
> 
>         Great answers in Stack Overflow, including a bash-only solution.
> 
>         http://stackoverflow.com/questions/15060384/one-liner-in-bash-using-perl-or-awk-to-change-extension-of-multiple-files
> 
>         On 4/6/16 5:52 PM, leam hall wrote:
>         > I'm trying to do something simple, change the ending of a script to
>         > ".txt". So if it's my_script.sh it becomes my_script.txt. Likewise for
>         > my_script.rb, etc. The .txt version will have the documentation and
>         > comments.
>         >
>         > So far all I've some up with is:
>         >
>         >   IS_SH=`echo ${SCRIPTNAME} | grep -c sh$`
>         >
>         > For each expected script ending. Which seems a really ugly thing to do.
>         > Is there a better way in Bourne shell to do this?
>         >
>         > Leam
> 



More information about the Ale mailing list