Shell Scripts (Linux) Write a script called vitest which uses a here document to
ID: 3871631 • Letter: S
Question
Shell Scripts (Linux)
Write a script called vitest which uses a here document to send several lines of text into a vi session. The script will have one mandatory command line argument which will be the name of the file that vi will open (e.g. You would type vitest file1.txt to invoke the script). If the user does not give the mandatory argument, echo out the usage for the script and exit. You will need to send an 'i' (insert mode), two lines of text and ESC ZZ (hit CTRL-v and ESC to insert an ESC character) to exit for your here document. Exec vi using the here document as input.
Explanation / Answer
Please find the code below :
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Usage: vitest.sh filename"
exit
fi
TARGETFILE=$1
vi $TARGETFILE <<EOF
i
This is line 1 of the example file.
This is line 2 of the example file.
Z
EOF
exit
Output
/cs/insight/rel/vivek_test$/> cat filename.txt
This is line 1 of the example file.
This is line 2 of the example file.
Please let me know if you face any difficulty understanding the code.
please vote up if satisfied.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.