Author Topic: [SOLVED] Creating a script  (Read 1212 times)

0 Members and 1 Guest are viewing this topic.

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
[SOLVED] Creating a script
« on: November 18, 2013, 02:23:57 am »
I have set myself a task, I want to create a script to read a text file that contains web addresses and download the file that the address points to and then loop round until it has downloaded the last one.

I'm not sure how to do it so I am reading a few books on the subject.
« Last Edit: November 18, 2013, 01:49:47 pm by pooky2483 »

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Creating a script
« Reply #1 on: November 18, 2013, 02:43:36 am »
As a start, I'll try and create one to read the file and display each line and wait for a keypress before getting the next one.

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Creating a script
« Reply #2 on: November 18, 2013, 01:01:35 pm »
I did a bit of searching online too. I didnt realise how easy it was.

Code: [Select]
while read LINE; do echo "$LINE"; done < ~/Peer_Bloclist.txt
Prints the web addresses onscreen.

Code: [Select]
 while read LINE; do wget $LINE; done < ~/Peer_Blocklist.txt

Works  :)
But what's the command to make it wait a few seconds and then continue?

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13185
  • Karma: 321
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Creating a script
« Reply #3 on: November 18, 2013, 01:10:53 pm »
sleep 5
or
sleep 10
etc.

maybe ?
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Creating a script
« Reply #4 on: November 18, 2013, 01:14:06 pm »
done it. I put in sleep 3 and its working OK

I cant believe how simple and small it is compared to what I thought it'd be.

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Creating a script
« Reply #5 on: November 18, 2013, 01:16:45 pm »
The reason behind it is, It took ages to download each one manually. So I wanted to automate it.
I've included the list.


Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: [SOLVED] Creating a script
« Reply #6 on: November 18, 2013, 09:27:31 pm »
The problem is, understanding the logic
e.g.
Code: [Select]
for file
do
    pr file > $file.tmp
done
The 'for' ; what exactly does it do to file, or to that matter any other variable to know how many times to loop?



Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13185
  • Karma: 321
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: [SOLVED] Creating a script
« Reply #7 on: November 19, 2013, 12:05:29 am »
Look at this:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

and these bash "for loop"examples
http://www.cyberciti.biz/faq/bash-for-loop/

Though I'm no programmer it appears to just define what to loop.
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: [SOLVED] Creating a script
« Reply #8 on: November 19, 2013, 04:15:12 am »
ta Mark  :)
Should help me.

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline chemicalfan

  • Hero Member
  • *****
  • Posts: 559
  • Karma: 12
  • Gender: Male
  • I've been here a little while!
    • View Profile
    • Awards
Re: [SOLVED] Creating a script
« Reply #9 on: November 19, 2013, 09:14:04 am »
Far out, the power of Bash amazes me everytime I see it - in my head, I was already grabbing for a Python script!
(probably just me there  ::))

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: [SOLVED] Creating a script
« Reply #10 on: November 19, 2013, 02:04:47 pm »
Me too Chem, I was expecting to be writing a large script with loads of commands but the simplicity amazes me.
And, talking of Python, I'm trying to learn that too. When I was on Window$, I was wanting to learn a programming language but all I found was languages such as C+ and Visual Basic that costs hundreds of pounds - I didn't know of the free ones around as you expect everything to cost. Since moving to Ubuntu, it has really opened my eyes.

I have set myself a project in Python, when I can fully understand the commands. I am going to create an address book.

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: [SOLVED] Creating a script
« Reply #11 on: November 19, 2013, 02:13:32 pm »
Is there a way I can save the file to another directory.
Also, to make it more of a challenge, the script downloads, I think they are 'tar.gz' files - can I unpack the file to another directory;
~/.config/transmission/blocklists

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: [SOLVED] Creating a script
« Reply #12 on: November 19, 2013, 03:08:54 pm »
I've had a go myself...
Will this work?

while read LINE
# Change directory to where the file is to be downloaded
cd ~/Downloads/Blocklists
# Download the file
do wget $LINE
# Wait 3 seconds
sleep 3
# Unpack the file to Transmissions Blocklist folder
tar -xc *.tar ~/.config/transmission/blocklists
done < ~/Peer_Blocklist.txt

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13185
  • Karma: 321
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: [SOLVED] Creating a script
« Reply #13 on: November 19, 2013, 03:42:50 pm »
Dunno

One thing I do know though is the tar options -x and -c are "extract" and "create" .. so you don't want "-xc"

I think you're more after something like:-

Code: [Select]
for i in `ls -1 *.tar`; do tar -xf $i -C $HOME/.config/transmission/blocklists; done

Which should unpack all .tar files in the current directory to ~/.config/transmission/blocklists

--
« Last Edit: November 20, 2013, 01:05:24 am by Mark Greaves (PCNetSpec) »
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1616
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: [SOLVED] Creating a script
« Reply #14 on: November 19, 2013, 03:51:36 pm »
I think you're more after something like:-

Code: [Select]
for i in `ls -1 *.tar`; do tar -xf $i -C $HOME/.config/transmission/blocklists; done

Which should unpack all .tar files in the current directory to ~/.config/transmission/blocklists

--

So, it unpacks the file and places it in the blocklists folder.
I an wanting to do additional tasks, such as rename it and include a number sequentially from 1 upwards.
Can I use the 'i' in the for and add it to a new name such as filename_$i.txt as the unpacked file ends with .unarchived

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-61-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal