Need help to get output of these two question please. I need step by step soluti
ID: 3606083 • Letter: N
Question
Need help to get output of these two question please. I need step by step solution with output please.(Unix)
9. Write a script that tests for at least 1 or more arguments in the line calling the script. If there are 0 arguments (use positional parameters), then notify user ofthe problem and exit status 1. Ifuser puts in an argument, echo program running" and exit with a status of0. 10. Write a shell script that copies the file named by its first argument (it must already exist) to a file with the same name with the filename extension of.bak. Thus, ifyou call the script with the argument first (and a file named first exists in the working directory), after the script runs you would have two files: first and first.bak Next, add a test routine to verify the user has used an argument that is a valid file andnot a directory. Ifit ot then quit the script with an 'exit 1Explanation / Answer
# Code for first part
#!/usr/bin/env bash
if [ -z "$1" ] # here checks for the arguement for string empty
then
echo "No argument supplied" #Priting message
exit 1
else
echo "Programming Running" # if there is arguement
fi
# end of first code
Output
user@pc:~$ sh test.sh
No argument supplied
user@pc:~$ sh test.sh hello
Programming Running
#second script
#!/bin/bash
FILE=$1
# echo -n "Enter target file name : "
# read targ
targ += ".bat"
if [ ! -f "$FILE" ] #checking if enetered input file exixts
then
echo "File $FILE does not exist"
else
# targ = "$1"
# echo "sucessful created "
targ=$1".bat" #adding extesnsion to file
echo "File name enetered is: " $1
cp "$1" "$targ"
fi
# store exit status of above cp command. It is use to
# determine if shell command operations is successful or not
status=$?
if [ $status -eq 0 ]
then
echo 'File copied successfully'
echo 'Newly created file name is: ' $targ
else
echo 'Problem copying file'
fi
#end of script
user@pc:~$ sh copy1.sh Poem.txt
copy1.sh: 6: copy1.sh: targ: not found
File name enetered is: Poem.txt
File copied successfully
Newly created file name is: Poem.txt.bat
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.