In: Computer Science
Task: 2.1 : Shell Scripting
Task1 :Traditional hello world script
# vi hello.sh
#!/bin/bash echo Hello World
How to Runhello.sh
Adding Execute Permission
# ls -la hello.sh
# chmod u+x hello.sh
# ls -la hello.sh
# ./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.
# vi calc.sh
#!/bin/bash #a simple script
echo "$1 + $2 = $(($1 + $2)) "
#chmod u+x calc.sh
# ./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
# vi myshell.sh
#!/bin/bash touch $1 ls > $1
# ./myshell.sh testfile
Task 2.4 : Write a script that will create a file using today’s date and the date format is ddmmmyy.dat
#!/bin/bash touch `date +%d%b%y`.dat FILENAME=`date +%d%b%y`.dat touch $FILENAME
#./date.sh
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
# vi creafile.sh
#!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created”
#./creafile.sh
Task 2.6 : In this exercise, you will create a script to echo users, based on a list of usernames in a file.
#viuserlist
moe larry curly
binny
#!/bin/bash
for TheUSER in $(cat userlist) do
echo The user is $TheUSER
done
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