Question

UNIX/LINUX Help. I need to modify this script so that the output of the file name...

UNIX/LINUX Help. I need to modify this script so that the output of the file name is changed from you_someAssignment.txt to You.someAssignment.txt.

#!/bin/sh

if [ $# -eq 1 ]; then
   STUDENTNAME=$USER
   ASSIGNMENT=$1
else
   echo "usage: `basename $0` [assignment]"
   exit 1
fi

if [ ! -d "$ASSIGNMENT" ]; then
  # Control will enter here if $ASSIGNMENT doesn't exist.
  mkdir $ASSIGNMENT
fi

# Move into the directory
cd $ASSIGNMENT

# Variable for the filename
FILENAME=$USER\_$ASSIGNMENT.txt

HOST=$(hostname)

DATE=$(date +"%m-%d-%Y")
TIME=$(date +"%r")

if [ -f "$FILENAME" ]; then
        sed -i "2s/.*/$HOST/g" "$FILENAME"
        sed -i "3s/.*/$DATE - $TIME/g" "$FILENAME"
else 
        printf "%s\n" "$USER" > $FILENAME
        printf "%s\n" "$HOST" >> $FILENAME
        printf "%s" "$DATE" >> $FILENAME
        printf " - %s\n" "$TIME" >> $FILENAME
fi
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the modified code. The changed line is bolded.


#!/bin/sh

if [ $# -eq 1 ]; then
STUDENTNAME=$USER
ASSIGNMENT=$1
else
echo "usage: `basename $0` [assignment]"
exit 1
fi

if [ ! -d "$ASSIGNMENT" ]; then
# Control will enter here if $ASSIGNMENT doesn't exist.
mkdir $ASSIGNMENT
fi

# Move into the directory
cd $ASSIGNMENT

# Variable for the filename
FILENAME=$USER.$ASSIGNMENT.txt

HOST=$(hostname)

DATE=$(date +"%m-%d-%Y")
TIME=$(date +"%r")

if [ -f "$FILENAME" ]; then
sed -i "2s/.*/$HOST/g" "$FILENAME"
sed -i "3s/.*/$DATE - $TIME/g" "$FILENAME"
else
printf "%s\n" "$USER" > $FILENAME
printf "%s\n" "$HOST" >> $FILENAME
printf "%s" "$DATE" >> $FILENAME
printf " - %s\n" "$TIME" >> $FILENAME
fi

Add a comment
Know the answer?
Add Answer to:
UNIX/LINUX Help. I need to modify this script so that the output of the file name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Install the supplied bash script on the Kali Linux VM BASH filename :ipLAN In text...

    1. Install the supplied bash script on the Kali Linux VM BASH filename :ipLAN In text editor looks like this #!/bin/bash is_alive_ping() {    ping -c 1 $1 >/dev/null 2 >&1    [ $? -eq 0 ] && echo Node with IP: $1 is UP. } for i in 192.168.1.{1..255} do is_alive_ping $1 & disown done QUESTION: Install the supplied bash script on the Kali Linux VM **NOte how do i install the above script in kali ,when i am...

  • I need a simple explanation of WHY the output is what it is -- Unix/Linux shell...

    I need a simple explanation of WHY the output is what it is -- Unix/Linux shell programming I am taking this introductory course in college echo * CIS132 ** echo "\\" echo $t1 variable t1=$t{1}\!\\n echo -e $t1 variable t1=$t{1}\!\\n Thanks for the explanations.

  • Hello I need help creating a bash script in linux for the following question: Create a...

    Hello I need help creating a bash script in linux for the following question: Create a script to expand the jpeg creator to create other types of files such as pdf, png, gif, etc. The jpeg creator is the following: #!/bin/bash FILE=$1 echo -e -n "\xFF\xD8\xFF\xE0"> $FILE dd if=/dev/random bs=512 count=4 >> $FILE

  • Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command no...

    Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command not found line 10: syntax error near unexpected token `then' line 10: `then' ------------------------------- #!/bin/bash if [ $# != 1 ] then echo "Usage: myScript.sh " exit 1 fi if [ -f $1 ] then awk ' BEGIN...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell...

    Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...

  • i need help with this scripting assignment in power shell thanks Lab 7: PowerShell Search and...

    i need help with this scripting assignment in power shell thanks Lab 7: PowerShell Search and Report Search Report host path date Execution time nnn Directories n, nnn, nnn Files nnnnnnn Sym links nnnnnnn Old files nnnnnnn Large files nnnn, nnn Graphics files nnnnnnn Executable files nnnnnnn Temporary files n, nnn, nnn TotalFileSize nnnn, nnn Introduction Lab 7 is nearly identical to Lab 2, except it is written in PowerShell, uses no UNIX/Linux tools to do its work, creates no...

  • Unix/Linux The purpose of this lab is to practice the commands to manage and organize files...

    Unix/Linux The purpose of this lab is to practice the commands to manage and organize files and directories: How would one go about this? Thanks. Task 1: Preliminaries: 1) If you have not already done so, create a directory called bin under your HOME directory. 2) If you have not already made a copy (using ftp) of the file called famous.dat from the Assignment#1, do that now. 3) Make bin the active/working directory. Task 2: Perform all of the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT