Question

In: Computer Science

Task: 2.1 : Shell Scripting Task1 :Traditional hello world script Open terminal create Lab01 directory and...

Task: 2.1 : Shell Scripting

Task1 :Traditional hello world script

  1. Open terminal create Lab01 directory and use cd command to move into the directory
  2. Open a new file script hello.sh using vi editor

# vi hello.sh

  1. Type the following lines

#!/bin/bash           echo Hello World    

  1. Save the file and return back to terminal

How to Runhello.sh

Adding Execute Permission

  1. Checking current permission       

# ls -la hello.sh

  1. Adding Execute permission

# chmod u+x hello.sh  

  1. Check whether execute permission is added

# ls -la hello.sh

  1. Running or executing the script  

# ./hello.sh

Description : This script has only two lines. The first indicates the system which program to use to run the file. The second line is the only action performed by this script, which prints 'Hello World' on the terminal.

Task2.2 :In this lab exercise, you will create a bash shell script and execute it. The studentswillalso use bash variables to customize their shell prompts.

  1. Open a new file script calc.sh using vi editor

# vi calc.sh

  1. Type the following lines

#!/bin/bash #a simple script

echo "$1 + $2 = $(($1 + $2)) "

  1. Save the file

  1. Change the permissions of calc.sh to executable with

#chmod u+x calc.sh

  1. From the terminal, run the script with  

# ./calc.sh 2 2

Task 2.3: Write a script that will create a file that is named when you execute the shell script and direct the o/p of ls to that file

  1. Open a new file script myshell.sh using vi editor

# vi myshell.sh

  1. Type the following lines

#!/bin/bash touch $1 ls > $1

  1. Add the execute permission
  2. Run the script

# ./myshell.sh testfile

  1. Check whether testfile is created
  2. Check the contents of the file
  3. Run the script again and create a testfile1

Task 2.4 : Write a script that will create a file using today’s date and the date format is ddmmmyy.dat

  1. Open a new file script date.sh using vi editor # vi date.sh
  2. Type the following lines

#!/bin/bash touch   `date +%d%b%y`.dat FILENAME=`date +%d%b%y`.dat touch $FILENAME

  1. Add the execute permission
  2. Run the script

#./date.sh

  1. Check whether file is created

Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created

  1. Open a new file script creafile.sh using vi editor

# vi creafile.sh

  1. Type the following lines

#!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created”

  1. Add the execute permission
  2. Run the script

#./creafile.sh

  1. Enter the file name you want to create
  2. Check whether file is created

Task 2.6 : In this exercise, you will create a script to echo users, based on a list of usernames in a file.

  1. create a new file called userlist.

#viuserlist

  1. Insert a short list of usernames, such as the following. Make sure that none are the names of existing users.

moe larry curly

binny

  1. create a script file userscript.sh and type the following lines

#!/bin/bash

for TheUSER in $(cat userlist) do

    echo The user is $TheUSER

done

  1. Add the permissions and run the script

Solutions

Expert Solution

Task: 2.1 : Shell Scripting

For insert the text in vi editor use i button to insert.

For saving the file in vi editor use :wq to save and quit from vi editor

File content of hello.sh is:

#!/bin/bash #a simple script

echo "$1 + $2 = $(($1 + $2)) "

Task2.2:

File content of calc.sh is:

#!/bin/bash #a simple script

echo "$1 + $2 = $(($1 + $2)) "

Task 2.3:

myshell.sh:

#!/bin/bash #a simple script

touch $1 ls > $1

Check the directory Lab01

testfile is crated

Task 2.4 :

date.sh file content is:

#!/bin/bash

touch `date +%d%b%y`.dat

FILENAME= `date +%d%b%y`.dat

$FILENAME

Task 2.5:

creafile.sh file content is:

#!/bin/bash

echo 'enter a file name: '

read FILENAME

touch $FILENAME

echo "$FILENAME has been created"

Task 2.6:

userlist file content is:

moe

larry

curly

binny

userlist.sh file content is:

#!/bin/bash

for TheUSER in $(cat userlist)

do

    echo The user is $TheUSER

done


Related Solutions

this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all its contents. Write a shell script to validate password strength. Here are a few assumptions for the password string.   Length – a minimum of 8 characters. • Contain alphabets , numbers , and @ # $ % & * symbols. • Include both the small and capital case letters. give a prompt of Y or N to try another password and displays an error...
Go to your “Documents” directory; a) Create a file “file1” with the text “hello” in it;...
Go to your “Documents” directory; a) Create a file “file1” with the text “hello” in it; Provide command and screenshot. b) Create a hard link named “file2” linked to “file1”; c) Create a soft link named “soft1” linked to “file1”; create another soft link named “soft2” linked to “file2”. d) View information of the 4 files (what command should you use to produce output) – how do you verify which file is a hard link and which file is a...
Use CYGWIN TERMINAL to create this script. COPY AND PASTE the screenshot. Script 1: 1.   Perform...
Use CYGWIN TERMINAL to create this script. COPY AND PASTE the screenshot. Script 1: 1.   Perform a ls -al on the files within the user's home directory and save it to a file called ls.dat within your ~ directory 2.   Save the tree information for the /home directory in a file called tree.dat within your ~directory 3.   Create a new directory in your home directory called "backups" 4.   Move the files you just created to the new directory 5.   Rename...
In this task, you will create a Python script in which you will practice reading files...
In this task, you will create a Python script in which you will practice reading files in Python and writing them to a new output file. Construct a text file called Py4_Task3_input.txt that has the following lines: 4 Sandwiches 04 July 2020 Pasta 31 October 2014 Hot Dogs 15 November 2005 Tacos 25 December 1986 The first line of the file represents the number of lines in the data file. Write a loop that reads in each additional line (one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT