Basic Shell Script for You
If you want to know who is the current logged in user:
echo “Currently logged on users:”
who
To find out the username:
echo "Active username : $(echo $USER)"
To know the current date and time:
echo "Current date and time : $(date)"
If you want to work with a file in the shell script
To know if the read file exists or not:
read input
if [ -f "$input" ]
then
# do something
else
# do something
fi
To check if the input value is string or number:
read read_input
if [[ $read_input == [0-9]* ]]
then
# it's number
else
# it's string
fi
Giving choice if you want to continue to do a certain work or not:
while true
do
echo -n "Do you want to continue y/n: "
read choice
if [ $choice == 'y' ]
then
continue
else
break
fi
done
If you want to read a text file line by line of what’s in it:
while IFS= read -r var
do
IFS=":" read -ra file_name <<< "${var}"
echo ${file_name}
done < "$input"
In the above code, “input” is the file to read. Each line is saved in “var” variable.
IFS saved each line after splitting the line by “:” Another example of how you can split a string line by a delimiter.
Well, that’s enough for a day to learn about the shell. Next time I will write something interesting. 😃