In: Computer Science
Write a Bash script clock.sh that once/second will print the time in the 24-hour clock format HH:MM:SS. See "man date" for a command to produce this format of time.
rubric
clock.sh prints h:m:s in 24-hour clock format
clock.sh prints time 1/second
clock.sh prints h:m:s in 24-hour clock format
Answer :
Step 1: First Create file clock.sh
Step 2 : chmod +x clock.sh (Change permission for executable)
Step 3: Open the file clock.sh and copy below thing
#date +"%H:%M:%S" ,specifies the format to display only hours minutes and seconds
date +"%H:%M:%S"
Step 4: Save the file
Step 5: Open the terminal and run below command in terminal
./clock.sh
Step 6: For exit from the program type Cntrl+D
Output :
clock.sh prints time 1/second
Answer :
Step 1: First Create file clock.sh
Step 2 : chmod +x clock.sh (Change permission for executable)
Step 3: Open the file clock.sh and copy below thing
#infinite loop
while true; do
#assigninig the value to the date variable
#date +"%H:%M:%S" ,specifies the format to display only hours minutes and seconds
date=$(date +"%H:%M:%S")
#printing the variable to the terminal
echo $date
#put thread to sleep for 1 sec
sleep 1
done
Step 4: Save the file
Step 5:Open the terminal and run below command in terminal
./clock.sh
Step 6: For exit from the program type Cntrl+D
Output :
Let me know if you need any help :)