LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2001, 12:15 AM   #1
bobthebat
Member
 
Registered: Apr 2001
Location: Portland, OR
Distribution: Slackware 8
Posts: 82

Rep: Reputation: 15
Smile


I am fairly new to Linux, and would like to get into shell scripting using bash. A script which I would like to write, and would also actually use would be one that would change the file extensions on a bunch of files in a directory. However I can't figure out how to do this. Anyone have any ideas?
 
Old 04-25-2001, 05:25 PM   #2
Celly
LQ Newbie
 
Registered: Apr 2001
Location: Dublin, Ireland
Posts: 2

Rep: Reputation: 0
Hi Bob & Everyone!!!
Bob, I am very new to linux too so, I can't help you, but I wanted to ask everyone for the same type of help so I thought i'd just tag along on your thread. What I need to do is format the output of ls -l to change the User to 'me' and the group to 'my group', any files ending in .html to name (html file) and files ending in .c or .cpp to name (c source file). Also, if the date of the file is today's date it should say 'today'. Most of these things I can hack out, but i've saved them in a text file called 'd' and made this executable, but when I go back to my terminal it says there's no command called 'd'. I wondered if anyone knew why it would do such a nasty thing to me. I've specified the c shell as the one for it to run in & this is my default shell anyway...

I'm so frustrated. Any help would go a long way...

Thanks everyone
Celine
 
Old 04-25-2001, 09:27 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Here is a simple script that will rename files with one extension to another. This one is simple and quick, but it could easily be modified to be more versatile. You could add command line options like -o and -n for old and new extension, possible a -R to recursively search subdirectories. The command line can be parsed with the getopts command in a bash shell, and the ls can be substituted with a find to search recrusivly. Since this is a simple script it does have a few problems. If the extension you are searching for occurs more than once in the file, then it will replace the first one it finds.

Code:
#!/bin/sh

OLD_SUB="hpp"
NEW_SUB="h"

for FILE in `ls *.$OLD_SUB`; do
   NEW_FILE=`echo $FILE | sed "s/$OLD_SUB/$NEW_SUB/"`
   echo $NEW_FILE
   mv $FILE $NEW_FILE
done

--As for Celly--

Sounds like 'sed' will be your tool of choice. Bash is much more powerfull when writing command scripts. csh is great for an interactive shell, but it lacks when writing scripts. Try executing your command file with . ./d
It is likely that a command inside the file is causing the error. If you were using bash, I would sugges that you run it in debug mode with: bash -x ./d

Good Luck

Gary
 
Old 04-26-2001, 02:02 PM   #4
Celly
LQ Newbie
 
Registered: Apr 2001
Location: Dublin, Ireland
Posts: 2

Rep: Reputation: 0
Hi Gary,

Thanks a mill. You're a pet. Just one ikkle question though 'cos I can't find out how to do this anywhere - like i've to change a few parts of the listing & I don't know how to group loads of 'sed' commands together...

Sorry to keep annoying you...

Thanks,
Celine
 
Old 04-26-2001, 09:46 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You can group sed commands together by using a sed command file. Just put all the command in the file and specify the option to sed to read the commands from the file. Check the man page for the correct flag.
Rather than using the sed file I would start by spliting out each field of the ls -l command into a variable or array index seen below in two seperate examples. Once it is split out, you can modify each element as needed. To find out the file type you can use grep and check the $? (return value) to see if the pattern matched. Check out the man page. Replacing the date with today may require a seperate print statement or something else. One thing you also have to consider is when a symbolic link shows up in the listing.

Examples: (very incomplete, just for ideas)

Code:
#!/bin/sh

declare -a LS_ARRAY

for FILE in  `ls`; do
   ENTRY=`ls -l $FILE`

   INDEX=0
   for FIELD in $ENTRY; do
      LS_ARRAY[$INDEX]=$FIELD
      echo ${LS_ARRAY[INDEX]}
      INDEX=`expr $INDEX + 1`

   done
done

# Next Example:

ME=`whoami`

for FILE in `ls`; do

   ENTRY=`ls -l $FILE`
   PERMS=`echo $ENTRY | cut -d' ' -f1`
   LINKS=`echo $ENTRY | cut -d' ' -f2`
   USER=`echo $ENTRY | cut -d' ' -f3`
   GROUP=`echo $ENTRY | cut -d' ' -f4`
   SIZE=`echo $ENTRY | cut -d' ' -f5`
   MONTH=`echo $ENTRY | cut -d' ' -f6`
   DAY=`echo $ENTRY | cut -d' ' -f7`
   YEAR_TIME=`echo $ENTRY | cut -d' ' -f8`
   FILE_NAME=`echo $ENTRY | cut -d' ' -f9`

   NEW_USER=`echo $USER | sed "s/$ME/me/"`

   printf "%s %s %-8s %-8s %9s\n" $PERMS $LINKS $NEW_USER $GROUP $SIZE
#  echo "$PERMS $LINKS $USER $GROUP $SIZE $MONTH $DAY $YEAR_TIME $FILE_NAME"

done
 
Old 05-05-2001, 08:37 AM   #6
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Bob and Celly, in the latest issue of Linux Magazine, Bill McCarty, author of Learning Linux from O'Reilly, has a column for newbies on shell scripting. You might want to check it out because in subsequent issues, he's going to elaborate a bit more.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with shell scripts thorney Linux - Newbie 4 11-27-2005 11:18 PM
Shell scripts??? F_ANTHONY Programming 2 10-27-2004 06:28 PM
Shell scripts anon227 Linux - Software 1 01-03-2003 03:11 PM
shell scripts nautilus_1987 Linux - General 3 08-30-2002 03:12 AM
shell scripts gui10 Programming 10 10-28-2001 02:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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