LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-09-2005, 03:05 AM   #1
CGameProgrammer
Member
 
Registered: Oct 2004
Distribution: Yoper
Posts: 30

Rep: Reputation: 15
My first shell script ever


Just wrote my first shell script, though it seems sort of sloppy to me. I wanted to rename 40-50 .html files to .txt, so I read a shell-scripting tutorial, learned a few new Linux commands, and eventually wrote this:
Code:
for F in `ls -a1 *.html`
    do
    wc=`echo -n $F | wc -m`
    len=`expr $wc - 5`
    p="%.`echo $len`s.txt\n"
    name=`printf $p $F`
    mv $F $name
    done
But because you can't nest backquotes, I used a bunch of those temporary variables (which I think looks sloppy) and when setting "p" you'll notice I used backquotes with an echo command, because it couldn't parse "%.$lens.txt\n". But that's an ugly hack IMO.

So, can someone suggest a better way of doing this sort of script?
 
Old 03-09-2005, 03:44 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
I think this should work.

Code:
#!/bin/bash

for F in `ls -a1 *.html`
    do
    mv $F `basename $F .html`.txt
done
 
Old 03-09-2005, 03:45 AM   #3
baltho
Member
 
Registered: Aug 2004
Location: Adelaide, South Australia
Distribution: Ubuntu (Natty)
Posts: 38

Rep: Reputation: 18
Hi there!!

In my experience, there's always at least a dozen ways to do the same thing in a shell script, so this is just what I'd do, but here goes...

for from in `ls *.html`
do
to=`echo $from | sed -e "s/.html\$/.txt/"`
mv $from $to
done

I find sed quite fiddly to work with, but VERY useful - tweaking the sed command and "echo"ing the mv command until I get it right is how I do it.

Hope it helps......
 
Old 03-09-2005, 09:35 AM   #4
CGameProgrammer
Member
 
Registered: Oct 2004
Distribution: Yoper
Posts: 30

Original Poster
Rep: Reputation: 15
Ah, basename is a very easy way to do it -- wish I'd known about it. Never heard of the sed command before, either; I'll have to look up how to use it.
 
Old 03-09-2005, 09:40 AM   #5
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
3 usefull commands to learn... i'm still learning them

grep, sed, awk
 
Old 03-09-2005, 11:25 AM   #6
CGameProgrammer
Member
 
Registered: Oct 2004
Distribution: Yoper
Posts: 30

Original Poster
Rep: Reputation: 15
I use grep alot, but I've never used those other two.
 
Old 03-11-2005, 04:43 PM   #7
tokiko
LQ Newbie
 
Registered: Oct 2003
Posts: 1

Rep: Reputation: 0
you don't even need a shell script; just use the "rename" command :P

rename .html .txt *
 
Old 03-11-2005, 05:17 PM   #8
CGameProgrammer
Member
 
Registered: Oct 2004
Distribution: Yoper
Posts: 30

Original Poster
Rep: Reputation: 15
Wow, lol. Linux has too many obscure commands.
 
Old 03-11-2005, 06:30 PM   #9
AxeZ
Member
 
Registered: Sep 2002
Location: Novi Sad, Vojvodina
Distribution: Slackware, FreeBSD
Posts: 386

Rep: Reputation: 32
There is no such thing as "too many"...
 
  


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
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

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

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