Write a script called activity4.1-3 that would help practice set and shift as fo
ID: 3785253 • Letter: W
Question
Write a script called activity4.1-3 that would help practice set and shift as followed:
1. Set the positional parameters to be the output of the date
2. Display the list of positional parameter
3. Display a message in the format
“today is <weekday>, the <calendarday> th of <month> <year> ”. Example: Today is Tuesday, the 17th of Mar 2015
4. Set the positional parameters to be the output of the file attributes for the file this.dir
5. Define a variable access to be the first parameter
6. Shift 9 positions
7. Display the contents of $1 and the variable acccess
Include a rst line that calls the Korn shell as the interpreter Add a comment to state the purpose of the script and other comments to make the script easier to read
Explanation / Answer
main.sh
--------------
#!/usr/bin/ksh
echo $1
echo $2
echo $3
echo "today is WEEKDAY, the DAYth of YEAR" | sed 's/WEEKDAY/'$1'/g' | sed 's/DAY/'$2'/g' | sed 's/YEAR/'$3'/g'
Description:
-----------------
1. To run the above script, first, you need to save the content in file like main.sh
2. Provide a execute access to main.sh file using command: chmod +777 main.sh
3. Execute the main.sh with all the positional parameters ( in our case there are three parameters)
i. e Command : . main.sh <WEEKDAY> <DAY> <MONTH_AND_YEAR>
. main.sh "Tuesday" "17" "Mar 2015"
4. In above script, i have sed command which is responsible for searching and replacing a particular value of placeholder. You can see my string was: "today is WEEKDAY, the DAYth of YEAR"
now first sed command will replace WEEKDAY from the first positional parameter which will pass using command execution i.e. Thursday
Similarly, once weekday replaced with variable value $1 (first positional parameter) then the replaceable string will be the input for another sed command which is used with pipeline symbol. now second sed command will replace another string DAY withe $2( second positional parameter) and the same replacement will happen for last and third positional parameter.
5. if you want to write the final output to some file then you can use ">>" operator with filename
i.e echo "today is WEEKDAY, the DAYth of YEAR" | sed 's/WEEKDAY/'$1'/g' | sed 's/DAY/'$2'/g' | sed 's/YEAR/'$3'/g' >> text.dir
you will get the replaceable string in the test.dir file in the same directory where this script file is placed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.