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

    Using sed to strip sftp from url

    I am trying to use a service menu that calsl a bash script and logs in to a remote server and executes a command on the file or folder. It should work but sed is not stripping sftp from the url path.
    Code:
    url=${1}
        echo "url: $url" # debug line
    
        if echo "$url" | grep -Ev "s+sftp://\w*@\w*/+/+"; then
    echo "Not a valid remote path: $url"
            return 1
        fi # This part checks for the sftp://user@host and returns if it is not found, this should stop it from running on files not accessed via sftp
    
        stripedurl=`echo "${url}" | sed 's+sftp://\w*@\w*(.\w*)?++'`
    OS: Kubuntu 12.10/Windows 8
    CPU: Intel Core i7 2600K
    Motherboard: Gigabyte GA-Z77X-UD5H
    Memory: 2x4GB Corsair Dominator
    Graphics Card: MSI R7770
    Monitor: Dell 2208WFP
    Mouse: Mionix NAOS 5000
    PSU: Corsair 520HX
    Case: Thermaltake Mozart TX
    Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
    Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

    #2
    Originally posted by Xplorer4x4 View Post
    I am trying to use a service menu that calsl a bash script and logs in to a remote server and executes a command on the file or folder. It should work but sed
    Code:
        stripedurl=`echo "${url}" | sed 's+sftp://\w*@\w*(.\w*)?++'`
    Is that supposed to do something like this?:
    Code:
    stripedurl=`echo "${url}" | sed "s/^sftp:\/\///g"`

    Comment


      #3
      It is ment to strip sftp://user@hostname.domain/ from the start of a path not just the sftp:// part.

      Comment


        #4
        Originally posted by james147 View Post
        It is ment to strip sftp://user@hostname.domain/ from the start of a path not just the sftp:// part.
        Right you are, of course

        It would probably work with 'sed -r'
        Last edited by kubicle; Oct 20, 2012, 02:40 PM.

        Comment


          #5
          Originally posted by kubicle View Post
          It would probably work with 'sed -r'
          Ahh, yes, I was swearing that the regex was correct but couldn't figure out why it didn't work... I should really read the man pages more often.

          Code:
          stripedurl=`echo "${url}" | sed -r 's+sftp://(\w*@)?\w*(\.\w*)?++'`
          Is what you want to use Xplorer4x4, that should reduce all of the following:
          sftp://user@host.domain/a/path/on/the/server
          sftp://user@host/a/path/on/the/server
          sftp://host.domain/a/path/on/the/server
          sftp://host/a/path/on/the/server
          to
          /a/path/on/the/server
          Last edited by james147; Oct 20, 2012, 03:46 PM.

          Comment


            #6
            Another option would be something like:
            Code:
            stripedurl=`echo "${url}" | sed "s/^sftp:\/\/[^\/]*//"`
            or (for easier readability):
            Code:
            stripedurl=`echo "${url}" | sed "s+^sftp://[^/]*++"
            or:
            Code:
            stripedurl=`echo "${url}" | sed "s+^.*//[^/]*++"`
            (I'm sure a ninja could make it shorter still )
            Last edited by kubicle; Oct 20, 2012, 03:54 PM.

            Comment


              #7
              Originally posted by kubicle View Post
              Another option would be something like:
              Code:
              stripedurl=`echo "${url}" | sed "s/^sftp:\/\/[^\/]*//"`
              That would be better, though I tend to replace the seperator character with one that doesn't apear so often in the pattern like:
              Code:
              stripedurl=`echo "${url}" | sed "s=^sftp://[^/]*=="`
              as it makes reading it easier.

              To be honest I am not sure why I didn't jump to this solution right away now should teach me to not pay that much attention to what I write.
              Last edited by james147; Oct 20, 2012, 03:51 PM.

              Comment


                #8
                Not exactly what you asked, Xplorer4x4, but why use subprocesses and pipes when bash can do it natively and cleanly:
                Code:
                strippedurl=/${url#sftp://*/}
                One sees that sed stuff a lot in Gnu scripts which have to run on the old Bourne shell, and can't use bashisms. People hack configure scripts and pick up practices like that from 20 years ago. However, the ${x#removed} construct is POSIX IIUC.
                Regards, John Little

                Comment


                  #9
                  I tried everything here and they still echo back the entire protocol/url. I am wondering if theres an error else where. Whole script:

                  Code:
                  #!/bin/bash
                  mktorrent() {
                      url=${1}
                      echo "url: $url" # debug line
                  
                      if echo "$url" | grep -Ev "s=^sftp://[^/]*=="; then
                  echo "Not a valid remote path: $url"
                          return 1
                      fi # This part checks for the sftp://user@host and returns if it is not found, this should stop it from running on files not accessed via sftp
                  
                      stripedurl=`echo "${url}" | sed "s=^sftp://[^/]*=="
                  
                      echo "stripedurl: $stripedurl" # debug line
                  
                      if ssh user@host "mktorrent -a 'http://site/key/announce' -p -l 21 '${stripedurl}'"; then
                  kdialog --passivepopup "Completed mktorrent" 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 "${1}" ] ; then
                  echo "Not enough arguments, exiting"
                      exit 1
                  fi
                  
                  for file in ${@} ; do
                  mktorrent ${file}
                  done
                  OS: Kubuntu 12.10/Windows 8
                  CPU: Intel Core i7 2600K
                  Motherboard: Gigabyte GA-Z77X-UD5H
                  Memory: 2x4GB Corsair Dominator
                  Graphics Card: MSI R7770
                  Monitor: Dell 2208WFP
                  Mouse: Mionix NAOS 5000
                  PSU: Corsair 520HX
                  Case: Thermaltake Mozart TX
                  Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                  Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                  Comment


                    #10
                    Originally posted by Xplorer4x4 View Post
                    I tried everything here and they still echo back the entire protocol/url. I am wondering if theres an error else where.
                    I think your problem might be your if statement for checking path validity...you are also missing the final backtick on your stripedurl, but I'd estimate your script might exit before that happens:

                    test example:
                    Code:
                    #!/bin/bash
                    mktorrent() {
                        url=${1}
                        echo "url: $url" # debug line
                    
                    case $url in
                      "sftp://"*) echo "$url is sftp";;
                      
                      *) echo "not sftp, exiting"; return 1 ;;
                    esac
                    
                    
                        stripedurl=`echo "${url}" | sed "s=^sftp://[^/]*=="`
                    
                        echo "stripedurl: $stripedurl" # debug line
                     
                    exit 0 # EXITING FOR TESTING PURPOSES ONLY
                        if ssh user@host "mktorrent -a 'http://site/key/announce' -p -l 21 '${stripedurl}'"; then
                    kdialog --passivepopup "Completed mktorrent" 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 "${1}" ] ; then
                    echo "Not enough arguments, exiting"
                        exit 1
                    fi
                    
                    for file in ${@} ; do
                    mktorrent ${file}
                    done
                    Results:
                    ./test sftp://user@hostname.domain/path/to/file
                    url: sftp://user@hostname.domain/path/to/file
                    sftp://user@hostname.domain/path/to/file is sftp
                    stripedurl: /path/to/file
                    @jlittle: people that think outside the box are unnerving

                    Comment


                      #11
                      The result was a little different then the first.
                      url: sftp://user@hostname.domain/path/to/file
                      sftp://user@hostname.domain/path/to/file is sftp
                      stripedurl: /path/to/file
                      There was no "sftp://user@hostname.domain/path/to/file" like you had one line one of the first result, and I see no torrent file created.
                      OS: Kubuntu 12.10/Windows 8
                      CPU: Intel Core i7 2600K
                      Motherboard: Gigabyte GA-Z77X-UD5H
                      Memory: 2x4GB Corsair Dominator
                      Graphics Card: MSI R7770
                      Monitor: Dell 2208WFP
                      Mouse: Mionix NAOS 5000
                      PSU: Corsair 520HX
                      Case: Thermaltake Mozart TX
                      Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                      Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                      Comment


                        #12
                        Originally posted by Xplorer4x4 View Post
                        There was no "sftp://user@hostname.domain/path/to/file" like you had one line one of the first result
                        I'm not sure I understand what you mean...could you elaborate?

                        and I see no torrent file created.
                        Did you remove the "exit 0 # EXITING FOR TESTING PURPOSES ONLY" line? I just added it to exit before actually doing anything.

                        Comment


                          #13
                          Originally posted by kubicle View Post
                          I'm not sure I understand what you mean...could you elaborate?
                          Sorry but I am not sure how I can clarify it any more. Did you check the output with in the quote? That's the out put exactly as it is echoed through konsole(modified path of course).

                          Did you remove the "exit 0 # EXITING FOR TESTING PURPOSES ONLY" line? I just added it to exit before actually doing anything.
                          Nope, sorry I saw that but didn't examine it closely and thought the whole line was commented out.

                          I tested it though and it worked! Thank you so much! I been working on this off and on for a few months.
                          OS: Kubuntu 12.10/Windows 8
                          CPU: Intel Core i7 2600K
                          Motherboard: Gigabyte GA-Z77X-UD5H
                          Memory: 2x4GB Corsair Dominator
                          Graphics Card: MSI R7770
                          Monitor: Dell 2208WFP
                          Mouse: Mionix NAOS 5000
                          PSU: Corsair 520HX
                          Case: Thermaltake Mozart TX
                          Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                          Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                          Comment


                            #14
                            Originally posted by Xplorer4x4 View Post
                            Sorry but I am not sure how I can clarify it any more. Did you check the output with in the quote? That's the out put exactly as it is echoed through konsole(modified path of course).
                            The output you quoted looks to me very similar to my output...of course my first line is just me running the script: "./test sftp://user@hostname.domain/path/to/file" and not part of the output.

                            I tested it though and it worked!
                            Good to hear

                            Comment


                              #15
                              Ah I realized that the first part of that line was calling the script but the url is being passed from Dolphin so why would you call the url alongside the script? :s


                              Also, I was thinking I need to duplicate this one more time. Now I could make the script call from in the service menu file but would still have to scripts in the ServiceMenu folder. So what if did:
                              Code:
                              #!/bin/bash
                              mktorrent-1() {
                              loop here
                              }
                              
                              if [ -z "${1}" ] ; then
                              echo "Not enough arguments, exiting"
                                  exit 1
                              fi
                              
                              for file in ${@} ; do
                              mktorrent-1 ${file}
                              done
                              Code:
                              mktorrent-2() {
                              loop here
                              }
                              
                              if [ -z "${1}" ] ; then
                              echo "Not enough arguments, exiting"
                                  exit 1
                              fi
                              
                              for file in ${@} ; do
                              mktorrent-2 ${file}
                              done
                              This is intended to be a single bash script just placed in 2 code tags for readability. Anyways would it be possible to execute the different loops from with in one service menu using 2 different exec lines like so:
                              Code:
                              Exec=/path/to/script.sh mktorrent-1 %U
                              Exec=/path/to/script.sh mktorrent-2 %U
                              This way depending on which service menu text I click, it executes either mktorrent-1 or mktorrent-2?
                              OS: Kubuntu 12.10/Windows 8
                              CPU: Intel Core i7 2600K
                              Motherboard: Gigabyte GA-Z77X-UD5H
                              Memory: 2x4GB Corsair Dominator
                              Graphics Card: MSI R7770
                              Monitor: Dell 2208WFP
                              Mouse: Mionix NAOS 5000
                              PSU: Corsair 520HX
                              Case: Thermaltake Mozart TX
                              Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                              Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                              Comment

                              Working...
                              X