Read User Input On Bash Scripting

The read statement is used for catch user input on the terminal when you run the script on your terminal. For example is just like input method which can get the information or data that user typed on the apps.

It's will work like this :
read -p "Please enter your name : " name

It's will appear on your screen when you run the that and when you typed your name into the terminal then your name will be saved into variable called name

Now we will practice to create one bash script used for see the file size using du command. The script will execute du command on the directory that you key in.

Using nano or any text editor that you comfy to use that. Put this script inside the file called script.sh

#!/bin/bash

read -p "Enter a directory path : " dir
start=$(date)
echo "Document directory usage report" > /tmp/report
du -sh $dir >> /tmp/report
echo "Start of report: $start" >> /tmp/report
echo "End of report: $(date)" >> /tmp/report

And then try to give permission to script using chmod.
andre@localhost:~$ chmod u+x script.sh

Then run the script. It's will asking you to enter directory path that you would see the size. 
andre@localhost:~$ ./script.sh
Enter a directory path : /etc/apt/ 

Then this script will earn new file called report, you can use reader more, cat or less to see the file.
andre@localhost:~$ more /tmp/report 
Document directory usage report
4.0K    /etc/apt
Start of report: Mon May 15 07:03:56 UTC 2020
End of report: Mon May 15 07:03:56 UTC 2020

Tulis komentar anda... Conversion Conversion Emoticon Emoticon

Thanks for your comment