Migrating Bash Script Functionality To Powershell

Welcome Forums General PowerShell Q&A Migrating Bash Script Functionality To Powershell

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #178638

    Participant
    Topics: 1
    Replies: 2
    Points: 17
    Rank: Member

    Hi guys – I'm looking to create a script that runs when using Transmission torrent client on Windows.

    The script runs after something has downloaded and removes any completed downloads from the queue automatically.

    Currently there is already a bash script available for Linux users, I'd like to provide the same functionality with Powershell.

    Here's the bash script https://pastebin.com/0neMUeKX

    I'm struggling to get similar functionality with powershell mostly with the following line:-

    sed -e '1d' -e '$d' | awk '{print $1}' | sed -e 's/[^0-9]*//g'`

    Anyone with better knowledge of Powershell who could assist would be greatly appreciated!

    Many thanks

     

  • #178671

    Participant
    Topics: 1
    Replies: 1547
    Points: 2,673
    Helping Hand
    Rank: Community Hero

    As bash code is everything else than descriptive I could imagine that not all of us Powershellers can read and understand bash code. Why don't you describe what your bash code is supposed to do and we might be able to recommend what Powershell equivalent you could use? 😉

    • #178689

      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      The comments in the script outline what the function is.  Essentially it calls transmission-remote to list current torrents, check if they are currently downloading and purge from the list if completed.

       

  • #178683
    js

    Participant
    Topics: 24
    Replies: 688
    Points: 1,690
    Helping Hand
    Rank: Community Hero

    Um, hey look. This is the answer.

    '1first line','2second line','3third line','4fourth line' | 
    
    # sed -e '1d' -e '$d' | 
    select -skip 1 | select -skiplast 1 | # delete first and last line
    
    # awk '{print $1}' | 
    foreach { (-split $_)[0] } | # split line and take only first field
    
    # sed -e 's/[^0-9]*//g'`
    foreach { $_ -replace '[^0-9]*' } # delete all non-nummbers
    

    Output:

    2
    3
    
  • #178749

    Participant
    Topics: 1
    Replies: 2
    Points: 17
    Rank: Member

    Um, hey look. This is the answer.

    PowerShell
    11 lines

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    '1first line','2second line','3third line','4fourth line' |
    # sed -e '1d' -e '$d' |
    select skip 1 | select skiplast 1 | # delete first and last line
    # awk '{print $1}' |
    foreach { (-split $_)[0] } | # split line and take only first field
    # sed -e 's/[^0-9]*//g'`
    foreach { $_ -replace '[^0-9]*' } # delete all non-nummbers
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Output:

    PowerShell
    3 lines

    1
    2
    3
    2
    3
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Thanks js! – really appreciate your help.

You must be logged in to reply to this topic.