Announcement

Collapse
No announcement yet.

Using sed to strip sftp from url

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • kubicle
    replied
    Originally posted by jlittle View Post
    Again, using bash only (to continue thinking out of the box)
    Code:
    strippedurl=/${url#sftp://*/}
    strippednospc=${strippedurl// /%20}
    the latter replacement should probably be the other way around in this case:
    Code:
    strippednospc=${strippedurl//%20/ }
    and I'm not 100% sure, but I think the latter replacement is a bashism (of course it's unlikely to matter much since the script uses #!/bin/bash)

    So either the second sed rule in my previous post or jlittle's shell built-ins should accomplish the same result (but you'll want to use ${strippednospc} in your mktorrent command if you choose the latter).

    Leave a comment:


  • jlittle
    replied
    Again, using bash only (to continue thinking out of the box)
    Code:
    strippedurl=/${url#sftp://*/}
    strippednospc=${strippedurl// /%20}

    Leave a comment:


  • kubicle
    replied
    Originally posted by Xplorer4x4 View Post
    It went great...unless it is dealing with spaces. If the file name or folder has a space in it, the spaces are converted to %20 rather then escaped.
    would adding another sed rule help:
    Code:
    stripedurl=`echo "${url}" | sed "s=^sftp://[^/]*== ; s=%20= =g"`
    if I misunderstood your problem, please elaborate (once again )

    Leave a comment:


  • Xplorer4x4
    replied
    Originally posted by kubicle View Post
    Let us know how it goes...some things are really easy to miss, that's why you can never have too many debug lines when writing a script :P...you can always comment out or delete the lines after you've done. I've sometimes echoed each variable before it's used for anything, it can make spotting the problems a lot easier while you're getting it to work.
    It went great...unless it is dealing with spaces. If the file name or folder has a space in it, the spaces are converted to %20 rather then escaped. As for Echos, yeah James showed me the benefits of that the other day as I didn't think it would help here.

    Originally posted by james147 View Post
    And sometimes they are very hard to find like missing a ; from the end of a class definition in c++... dam thing gives a compile error in a different file!!
    lol I used to do some c++ work for a mod system for a game, and I know what ya mean.

    Leave a comment:


  • james147
    replied
    Originally posted by kubicle View Post
    Let us know how it goes...some things are really easy to miss,.
    And sometimes they are very hard to find like missing a ; from the end of a class definition in c++... dam thing gives a compile error in a different file!!

    Leave a comment:


  • kubicle
    replied
    Originally posted by Xplorer4x4 View Post
    Wow, maybe I really need to sleep(8 hours in 2 days). I will test that later or tomorrow and report back if I find any trouble. Otherwise, thank you to everyone that contributed and helped me learn some new tricks to my arsenal.
    Let us know how it goes...some things are really easy to miss, that's why you can never have too many debug lines when writing a script :P...you can always comment out or delete the lines after you've done. I've sometimes echoed each variable before it's used for anything, it can make spotting the problems a lot easier while you're getting it to work.

    Leave a comment:


  • Xplorer4x4
    replied
    Originally posted by kubicle View Post
    use:
    Code:
    maketorrent ${anounce} ${file}
    instead of
    Code:
    maketorrent ${file}
    when you call the maketorrent function, so the function gets two arguments (otherwise there is nothing in $2 inside the function)
    Wow, maybe I really need to sleep(8 hours in 2 days). I will test that later or tomorrow and report back if I find any trouble. Otherwise, thank you to everyone that contributed and helped me learn some new tricks to my arsenal.


    Sent from my DROID2 Global

    Leave a comment:


  • kubicle
    replied
    use:
    Code:
    maketorrent ${anounce} ${file}
    instead of
    Code:
    maketorrent ${file}
    when you call the maketorrent function, so the function gets two arguments (otherwise there is nothing in $2 inside the function)

    Leave a comment:


  • Xplorer4x4
    replied
    Originally posted by james147 View Post
    the problem now is that url${2} is inside the function, so that means the second argument passed to that function, in the forloop we used $@ to loop through all the arguments and pass them to the function, what you want to do is this:
    Code:
    anounce=${1}
    shift # removes $1 and makes $2 become $1
    for file in $@; do
        mktorrent ${anounce} ${file}
    done
    This will get the first argument, store it in $anounce then shift all the arguments left one ($2 becomes $1, $3 becomes $2 etc) then loops over the remaining arguments and passes $anounce and one argument at a time to the mktorrent function.
    Maybe I missed something but same error, script looks like:
    Code:
    #!/bin/bash
    maketorrent() {
        url=${2}
    
    case $url in
      "sftp://"*) echo "$url is sftp";;
    
      *) echo "not sftp, exiting"; return 1 ;;
    esac
    
    
        stripedurl=`echo "${url}" | sed "s=^sftp://[^/]*=="`
    
        if ssh user@host "mktorrent -a '${1}' -p -l 21 '${stripedurl}'"; then
    	  kdialog --passivepopup "Completed making torrent for '$stripedurl" 60
        else
    	  kdialog --passivepopup "Failed mktorrent" 60
           return 1 # Change to exit 1 to stop the script on first failure
        fi
    
    echo "Finished" # debug line
    }
    
    if [ -z "${2}" ] ; then
    echo "Not enough arguments, exiting"
        exit 1
    fi
    
    anounce=${1}
    shift
    for file in $@ ; do
    maketorrent ${file}
    done

    Leave a comment:


  • SteveRiley
    replied
    Originally posted by kubicle View Post
    @jlittle: people that think outside the box are unnerving
    Yah, I saw that and I was like

    Leave a comment:


  • kubicle
    replied
    Good catch james...I knew I should have checked the whole script again rather than a few relevant lines, but of course I had one foot out-the-door already.

    Leave a comment:


  • james147
    replied
    the problem now is that url${2} is inside the function, so that means the second argument passed to that function, in the forloop we used $@ to loop through all the arguments and pass them to the function, what you want to do is this:
    Code:
    anounce=${1}
    shift # removes $1 and makes $2 become $1
    for file in $@; do
        mktorrent ${anounce} ${file}
    done
    This will get the first argument, store it in $anounce then shift all the arguments left one ($2 becomes $1, $3 becomes $2 etc) then loops over the remaining arguments and passes $anounce and one argument at a time to the mktorrent function.

    Leave a comment:


  • Xplorer4x4
    replied
    I tried that method, and it claims "not sftp, exiting" from the first echo debug line. I did what you said, in the service menu I used:
    Exec=maketorrent.sh "http://site/key/announce" %U
    Exec=maketorrent.sh "http://anothersite/key/announce" %U

    In the script:
    url=${2}
    if ssh user@host "mktorrent -a '${1}' -p -l 21 '${stripedurl}'"; then

    Leave a comment:


  • kubicle
    replied
    Originally posted by Xplorer4x4 View Post
    As for $1 vs $2, maybe it's lack of sleep, maybe not but I am not sure I follow, so is this right..
    Exec=maketorrent.sh mktorrent1 %U
    Means %U is the second variable so I would use $2? However if I use:
    Exec=maketorrent.sh %U
    Means %U is the first variable so $1?
    That's correct

    I was thinking, using 2 entire subrotunies is excessive as only the announce url will change. So if I nested the different announce urls in an if statement, this would mean the exec line needs to be:
    Exec=maketorrent.sh $mktorrent $announce1 %U
    Then url would become $3. Am I following you correctly or am I way off?
    If the only thing that changes is the announceurl, you could provide it directly from the exec lines:

    Exec=maketorrent.sh "http://site/key/announce" %U
    Exec=maketorrent.sh "http://anothersite/key/announce" %U

    And you wouldn't need any checks, you could just use $1 (which would contain the announce URL) in your script where you needed it, and the file url from dolphin (%U) would be in $2

    Leave a comment:


  • Xplorer4x4
    replied
    Re: Bash vs Perl, thanks for the answer. I was just curious. Always eager to soak up info.

    As for $1 vs $2, maybe it's lack of sleep, maybe not but I am not sure I follow, so is this right..
    Exec=maketorrent.sh mktorrent1 %U
    Means %U is the second variable so I would use $2? However if I use:
    Exec=maketorrent.sh %U
    Means %U is the first variable so $1?

    I was thinking, using 2 entire subrotunies is excessive as only the announce url will change. So if I nested the different announce urls in an if statement, this would mean the exec line needs to be:
    Exec=maketorrent.sh $mktorrent $announce1 %U
    Then url would become $3. Am I following you correctly or am I way off?

    Sent from my DROID2 Global

    Leave a comment:

Users Viewing This Topic

Collapse

There are 0 users viewing this topic.

Working...
X