LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-30-2001, 11:33 AM   #1
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
making my script generic


i'm trying to get a handy script to work nice and generically, and i'm stuck.

in the script, i want to use a sed command to change one parameter into another. I want to be able to write:

sed s/$1/$2/

but it doesn't work... i've tried every combinaion of quotes and such like, but not got anywhere. this sed command is in a variable assignment with back ticks, so using another backtick with echo stuff it up:

Code:
cd "$1"
for i in *
do
  if [ -d "$i" ]
  then
    newdirpath=`echo "$PWD/$i" | sed s/$1/$2/ | tr ' ' '_' | tr A-Z a-z | sed s/_-_/-/`
    # echo $newdirpath
    
    # find if dir has subdirectories
    for j in "$PWD/$i"/*  
    do
      if [ -d "$j" ]
      then
        mkdir -p "$newdirpath"
      fi
    done

    find "$PWD/$i" -iname '*.mp3' -fprint "$newdirpath".m3u 

    $0 "$i"     
  fi
done
cd -
so how do i make the first sed call generic???

------------------
edit
------------------

oh right, ok, that does actually work after all. My problem is that i want to include /'s in the variable, which is what causes the problem. i need / as i want to pass the script two directories eg /smb/mp3 and /clone/mp3. how can i do this??

Last edited by bluecadet; 12-30-2001 at 12:39 PM.
 
Old 12-31-2001, 03:34 PM   #2
WindozBytes
Member
 
Registered: Aug 2001
Location: CT, USA
Distribution: Mandriva 2008
Posts: 105

Rep: Reputation: 15
I don't use sed much, but I believe there is a way to use a character other than "/" for the delimiter of the substitution command. You could try something simple like s<$1<$2<. If that doesn't work, you may have to explicitly define the "<" as your delimiter or "escape" it in some way....

Last edited by WindozBytes; 12-31-2001 at 03:37 PM.
 
Old 01-16-2002, 12:31 PM   #3
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
The previous post is the better solution I'd guess, but if you still need the escape character, use '\'

eg.
echo "/usr/local" | sed -e 's/\//-/g'

replaces all forward slashes with dashes.

$var = `echo $1 | sed -e 's/\//\\\//g'`

escape all slashes before passing $var to the quoted sed command
 
  


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 in making script depam Linux - Software 1 10-19-2005 07:40 PM
making a bash script student04 Linux - Software 5 01-11-2005 03:41 AM
Making a new script Lostboys Slackware 9 09-07-2004 06:11 AM
making a script 'global' fatherg Linux - General 3 08-08-2004 07:40 AM
Anyone have a generic "MENU" script?? Shinobi Linux - General 1 11-14-2003 05:13 PM

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

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