Monday, 16 November 2020

Shell Script: Get the Average Grade of a Student

Write a shell script, which gives back the average grade of a student The student name we should give as a parameter! If you give a name that is not in the data file, please write out an error message and write out the list of the names (Just the names without the grades!!!) as a help!

input file: class.txt

Name/Math Informatics Literature English
Mohammed 3 4 5 4
Alan 2 1 3 2
Li 3 5 1 3
Jane 4 2 3 1

SCRIPT:

#!/bin/bash
# Variable Declaration
input_file="./class.txt"
name=$1
average=0
err_msg="\n\tPlease pass a student name to the script. \n\te.g.: ./average.sh Tom \n" 
name_list=`cat class.txt | awk '(NR != 1) {print $1}' | tr "\n" ","  | sed -e 's/,$//' -e 's/,/, /g' `
#---------------------------------------------------------------------------------------------
if [ -z "$name" ]
then
echo -e "$err_msg"
else
record=$(grep -w "$name" $input_file)
if [ -n "$record" ]
then
#sum=$(echo $a | awk '{sum=0; for (i=2; i<=NF; i++) { sum +=i} print sum }')
average=$(echo $record | awk '{sum=0; for (i=2; i<=NF; i++) { sum +=$i} print sum/(NF - 1) }')
echo -e "\t $name $average \n"
else
echo -e "No $name is in the list. There are: $name_list"
fi
fi


let's check the output:




Sunday, 8 November 2020

Shell Script: Write 000 to 999 (1000 lines in a file) using Brace Expansion

We need to write from 000 to 999 in a file. As these are consecutive numbers we will be using "Brace Expansion"

Let's Create a file by using the touch command.


Script:

 #create an empty file
touch output.txt 

#using brace expansion with foor loop
 for i in {000..999} #this is brace expansion
do
    #append data to output.txt 
    echo $i >> output.txt
done


Output: 

To see the output use bellow command

        cat output.txt | more 

Thursday, 5 November 2020

PowerShell: Remove Local User

 Read local user names from a file and remove them.

The file userFile.txt contains local user names to be removed from the computer.


$userData = Get-Content .\userFile.txt

foreach ($usr in $userData){
#remove users
Remove-LocalUser -Name "$usr"
}


Read:    How to add local user

Saturday, 31 October 2020

PowerShell: Write a Script to Add a local user

Create a group of users using a PowerShell script.

There is a file consisting of a group of 3 new user name’s and passwords in each line.

userFile.txt
Jack,123jack
Tim,123tim
Shawn,123shawn


 Write a script that does the following

i.      Retrieve the names and assign them to an array

ii.      Write a loop that prints out each of the user names. 

iii.      Create an account and Add each of these users to the local group  'Users'


PowerShell Script


#Use get-content to retrieve the names and assign them to an array
    $userData = Get-Content .\userFile.txt

#Write a loop that prints out each of the user names. 
    Write-Output "Below is the list of users:"
    foreach ($data in $userData){
        $user = ($data.split(","))[0]
        $user
    }

#create an account using localuser for each of the users, Add each of these users to the local group Users
    foreach ($data in $userData){
       $user = ($data.split(","))[0]
        $psswd = ($data.split(","))[1]
    
#convert password to  secure string
        $securPass=convertTo-SecureString -string $psswd -asPlainText -Force
#create new local user
        New-LocalUser -Name "$user"  -Password $securPass
#add user to local group Users
        Add-LocalGroupMember -Group "Users" -Member "$user"
}



You can verify the script by using the below cmdlet in PowerShell prompt:

        Get-LocalGroupMember -Name 'Users





Thursday, 29 October 2020

Shell Script: find the 5 most frequent words in an article

Shell Script to find the 5 most frequent words in an article.


Article:

The company's rapid growth since incorporation has triggered a chain of products, acquisitions, and partnerships beyond Google's core search engine (Google Search). It offers services designed for work and productivity (Google Docs, Google Sheets, and Google Slides), email (Gmail), scheduling and time management (Google Calendar), cloud storage (Google Drive), instant messaging and video chat (Duo, Hangouts, Chat, and Meet), language translation (Google Translate), mapping and navigation (Google Maps, Waze, Google Earth, and Street View), podcast hosting (Google Podcasts), video sharing (YouTube), blog publishing (Blogger), note-taking (Google Keep and Google Jamboard), and photo organizing and editing (Google Photos). The company leads the development of the Android mobile operating system, the Google Chrome web browser, and Chrome OS, a lightweight operating system based on the Chrome browser. Google has moved increasingly into hardware; from 2010 to 2015, it partnered with major electronics manufacturers in the production of its Nexus devices, and it released multiple hardware products in October 2016, including the Google Pixel smartphone, Google Home smart speaker, Google Wifi mesh wireless router, and Google Daydream virtual reality headset. Google has also experimented with becoming an Internet carrier (Google Fiber, Google Fi, and Google Station)


let's store the text in a file named 'google.txt'


Command:

cat google.txt | tr '[ :upper: ]' '[ :lower: ]' | grep -oE '\w+' | grep -vE "and|has|of|the|has|with|an" | sort | uniq -c | sort -nr | head -n 5


Output:



cat - to display the content of the file

tr '[ :upper: ]' '[ :lower: ]'  - convert all uppercase to lowercase

grep - to find particular words

sort - sort the list of words here

uniq -c  - it provides the count of unique words 

head - it provides the top few lines from the list or file


Wednesday, 28 October 2020

Simple parsing in PowerShell & Sorting

Take a variable and break it up into 2 parts, store it in a simple array, and display values using PowerShell. Also, sort the array in descending order.


Input: "Hello-World"
variable one would be from 1st character of the input and up to the "-"
variable two would be from the character "-" to the end of the input variable


so
Output 1 would be "Hello"
Output 2 would be "World"


Program:

$string = Read-Host "Enter a String" 
$varArray = $string.split("-")

#variable 1
$varArray[0]

#variable 2
$varArray[1]

#sorting the array
Write-Host "`nSorted Array in Descending Order:"
$varArray | Sort-Object -Descending