Situatie
Let there are some situations in which you have to save your output in a file ( generally called log file). Output can be user details ( username, password, Gmail, etc. ), products record ( buying or selling any goods ), or simply any kind of data that you can store in a log file.
Solutie
Pasi de urmat
In this example, we can store the names of two-person in a log file.
First, create a log file named ” Log.txt ” by the following command
$ touch Log.txt
Note: ” Log.txt ” will store the output.
Now, create a bash script file by the following command
$ touch Name.sh
Note: ” Name.sh ” It is a bash script file that will take two names from the user and store it into ” Log.txt “
Now, open ” Name.sh ” in a text editor
$ nano Name.sh
Now write the following bash script in ” Name.sh ”
#!/bin/bash echo "Enter First Person Name : " # It will take input from user i.e. # First Person Name read FName
# It store First Person Name in Log.txt echo "First Person Name : $FName">Log.txt echo echo "Enter Second Person Name : " # It will take input from user i.e. # Second Person Name read SName # It append Second Person Name in Log.txt echo "Second Person Name : $SName">>Log.txt
Note: Here, ‘ > ‘ it will create a new file with a specified name if the file does not exist and it will use to overwrite the data stored in the file ( i.e. ” Log.txt “). And ‘ >> ‘ it will create a new file with a specified name if the file does not exist and it will append the output data in the file ( i.e. ” Log.txt ” )
Now save and run ” Name.sh ” by the following command
$ chmod +x ./Name.sh $ ./Name.sh
As it is shown in the above picture that we take two inputs ” Sahil ” and ” Rakesh “. The output name will store in ” Log.txt “.
Leave A Comment?