User:Hleinola: Difference between revisions
From ICO wiki
Jump to navigationJump to search
Created page with '== Quest lahendus 37 - Skript, mis leiab etteantud kataloogist kõik katkised nimelingid [Bash] == <source lang="bash"> #!/bin/bash #Autor: Henrik Leinola #Versioon: 0.1 OODATUD…' |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 32: | Line 32: | ||
fi | fi | ||
exit 0 | exit 0 | ||
</source> | |||
==Powershell kodutöö== | |||
<source lang = "Powershell"> | |||
#Skript, eemaldab mp3 faili nimest alakriipsud ja muudab kõikide sõnade esimese tähe suureks | |||
#Autor: Henrik Leinola | |||
Param($Path) | |||
$Extension = "mp3" | |||
if (Test-Path $Path) | |||
{ | |||
cd $Path | |||
$AllFiles = Get-ChildItem . *.$Extension | % { [IO.Path]::GetFileNameWithoutExtension($_) } | |||
} | |||
else | |||
{ | |||
Write-Host "Sellist kausta ei eksisteeri" | |||
exit | |||
} | |||
foreach ($File in $AllFiles) { | |||
$String = $File.ToString() | |||
$String = $String -replace '_',' ' | |||
$String = $String.ToLower() | |||
$String = (Get-Culture).TextInfo.ToTitleCase($String) | |||
Rename-Item "$File.$Extension" "$String.$Extension" | |||
} | |||
cd .. | |||
</source> | |||
== Pythoni kodutöö == | |||
<source lang="Python"> | |||
#!/usr/bin/python | |||
# -*- coding: utf-8 -*- | |||
#Autor: Henrik Leinola | |||
#Skript, mis võtab teksti failist ees- ja perenime ning teeb nendest kasutaja nime, genereerib parooli ja loob vastava kasutaja ning väljastab tulemused uude tekstifaili. | |||
import sys, os, random | |||
NewName = True | |||
String = "" | |||
LowerCase = "abcdefhijklmnopqrstuv" | |||
UpperCase = "ABCDEFGHIJKLMNIPQRSTUV" | |||
Numbers = "1234567890" | |||
if os.getuid() != 0: | |||
print "Käivita skript juurkasutaja õigustes!" | |||
sys.exit(1) | |||
if len(sys.argv) == 3: | |||
SourceFile = sys.argv[1] | |||
OutFile = sys.argv[2] | |||
else: | |||
print "Kasuta skripti järgmiselt: " + sys.argv[0] + " <sisend fail> <väljund fail>" | |||
sys.exit(1) | |||
def GeneratePassword(): | |||
password = "" | |||
for i in range(1,12): | |||
rand = random.randrange(1,4) | |||
if rand == 1: | |||
rand = random.randrange(1,len(LowerCase)) | |||
char = LowerCase[rand] | |||
elif rand == 2: | |||
rand = random.randrange(1,len(UpperCase)) | |||
char = UpperCase[rand] | |||
else: | |||
rand = random.randrange(1,len(Numbers)) | |||
char = Numbers[rand] | |||
password = password + char | |||
return password | |||
fh = open(SourceFile) | |||
AllLines = fh.readlines() | |||
fh.close() | |||
fw = open(OutFile,"w") | |||
for line in AllLines: | |||
name = line.split() | |||
for part in name: | |||
if NewName: | |||
Counter = len(name) | |||
NewName = False | |||
String = (String + part[:4]) | |||
Counter = Counter - 1 | |||
if Counter == 0: | |||
NewName = True | |||
UserPassword = GeneratePassword() | |||
Write = (String + " " + UserPassword) | |||
fw.write(Write + "\n") | |||
os.system ("useradd -m -d /home" + String + " " + String + " ") | |||
os.system ("echo " + String + ":" + UserPassword + "| chpasswd ") | |||
String = "" | |||
fw.close() | |||
</source> | </source> |
Latest revision as of 11:23, 2 September 2011
Quest lahendus 37 - Skript, mis leiab etteantud kataloogist kõik katkised nimelingid [Bash]
#!/bin/bash
#Autor: Henrik Leinola
#Versioon: 0.1
OODATUD_ARGU=1
REKURSIIVSELT="-r"
die () {
echo >&2 "$@"
exit 1
}
if [ $# -lt $OODATUD_ARGU ]; then
die "Kasutamine:" $0 "ja kataloogi nimi."
fi
if [ ! -d $1 ]; then
die "Tegemist ei ole kataloogiga."
fi
if [ $# -gt $OODATUD_ARGU ]; then
if [ $2 == $REKURSIIVSELT ]; then
find $1 -type l ! -exec test -r {} \; -print 2>/dev/null
else
die "Rekursiivselt otsimiseks kasuta -r argumenti."
fi
else
ind $1 -maxdepth 1 -type l ! -exec test -r {} \; -print 2>/dev/null
fi
exit 0
Powershell kodutöö
#Skript, eemaldab mp3 faili nimest alakriipsud ja muudab kõikide sõnade esimese tähe suureks
#Autor: Henrik Leinola
Param($Path)
$Extension = "mp3"
if (Test-Path $Path)
{
cd $Path
$AllFiles = Get-ChildItem . *.$Extension | % { [IO.Path]::GetFileNameWithoutExtension($_) }
}
else
{
Write-Host "Sellist kausta ei eksisteeri"
exit
}
foreach ($File in $AllFiles) {
$String = $File.ToString()
$String = $String -replace '_',' '
$String = $String.ToLower()
$String = (Get-Culture).TextInfo.ToTitleCase($String)
Rename-Item "$File.$Extension" "$String.$Extension"
}
cd ..
Pythoni kodutöö
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Autor: Henrik Leinola
#Skript, mis võtab teksti failist ees- ja perenime ning teeb nendest kasutaja nime, genereerib parooli ja loob vastava kasutaja ning väljastab tulemused uude tekstifaili.
import sys, os, random
NewName = True
String = ""
LowerCase = "abcdefhijklmnopqrstuv"
UpperCase = "ABCDEFGHIJKLMNIPQRSTUV"
Numbers = "1234567890"
if os.getuid() != 0:
print "Käivita skript juurkasutaja õigustes!"
sys.exit(1)
if len(sys.argv) == 3:
SourceFile = sys.argv[1]
OutFile = sys.argv[2]
else:
print "Kasuta skripti järgmiselt: " + sys.argv[0] + " <sisend fail> <väljund fail>"
sys.exit(1)
def GeneratePassword():
password = ""
for i in range(1,12):
rand = random.randrange(1,4)
if rand == 1:
rand = random.randrange(1,len(LowerCase))
char = LowerCase[rand]
elif rand == 2:
rand = random.randrange(1,len(UpperCase))
char = UpperCase[rand]
else:
rand = random.randrange(1,len(Numbers))
char = Numbers[rand]
password = password + char
return password
fh = open(SourceFile)
AllLines = fh.readlines()
fh.close()
fw = open(OutFile,"w")
for line in AllLines:
name = line.split()
for part in name:
if NewName:
Counter = len(name)
NewName = False
String = (String + part[:4])
Counter = Counter - 1
if Counter == 0:
NewName = True
UserPassword = GeneratePassword()
Write = (String + " " + UserPassword)
fw.write(Write + "\n")
os.system ("useradd -m -d /home" + String + " " + String + " ")
os.system ("echo " + String + ":" + UserPassword + "| chpasswd ")
String = ""
fw.close()