Tuesday, 14 January 2014

Shell scripting basics

Display the text “Cyber Security” in the shell prompt. Redirect the displayed line to a file.
Append the outputs of the commands to show the users logged into your system, your
login account and the current date in the ddmmyyyy format to the file.

touch file echo "Cyber Security" >>file 
echo "Users logged in:" >>file w >>file 
echo "my account:" >>file 
who am i >>file 
echo "Today it is:" >>file 
date +%d-%m-%Y >>file 

The ls command with the –R option lists the files in the specified directory and also
subdirectories, if any. Use this with a suitable pipe and filter arrangement to determine
whether a file exists in the account logged in.

echo "Listing all directories and files" 
ls -R 
echo "Enter file name to search" 
read fname echo "Searching for $fname :" 
echo "Search results are below :" 
ls -R|grep -x "$fname" 

Assuming you have a directory containing the following files:
men, orange, book, ant, lotus, apple
Use the ls command to list only those files that
a) consist of five letters
b) begin with the letter l
c) end with the letter t
d) have the second letter o

mkdir myfolder 
cd myfolder 
touch men orange book ant lotus apple 
echo All files in the directory: 
ls 
echo Files with name starting with 'l' : 
ls l* 
echo Files with name ending with 't': 
ls *t 
echo Files having the second letter 'o': 
ls [a-z][o]* 
echo Files with name having 5 letters: 
ls ????? 


 

No comments:

Post a Comment