Unix Shell problems 1 Create a script that shows lines conta
Unix Shell problems:
1. Create a script that shows lines containing trailing white spaces from a file. This script should use a sed script and show sensible information to the user
2 For this exercise, your input is lines in the following form:
Username:Firstname:Lastname:Telephone number
Make an awk script that will convert such a line to an LDAP record in this format:
dn: uid=Username, dc=example, dc=com
cn: Firstname Lastname
sn: Lastname
telephoneNumber: Telephone number
3. Use an if/then/elif/else construct that prints information about the current month. The script should print the number of days in this month, and give information about leap years if the current month is February.
4. Edit the leaptest.sh script from Section 7.2.4 so that it requires one argument, the year. Test that exactly one argument is supplied.
Section 7.2.4 can be found
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html#sect_07_02_04
| Username:Firstname:Lastname:Telephone number |
Solution
1. Solution of first Problem
#!/bin/bash
#sample File location.
file = \"/dump/file\"
sed -n \'/ \\+$/p\' file
#end script
This will give proper output.Tested on linux machine.
root@mercury06> sh 1.sh
asasa
sdsdsd
Answer to second Problem:
#!/bin/bash
#sample File location.
line = Username:Firstname:Lastname:Telephone number
echo line | awk -F\':\' \'{print \"dn: uid: \"$1\"dc=example, dc=com\" \"\ cn: \"$2 $3 \"\ sn: \"$3 \"\ telephoneNumber: \"$4}\'
#end script
tested on linux machine. Giving same as desired.
root@mercury06> echo Username:Firstname:Lastname:Telephone number | awk -F\':\' \'{print \"dn: uid: \"$1\"dc=example, dc=com\" \"\ cn: \"$2 $3 \"\ sn: \"$3 \"\ telephoneNumber: \"$4}\'
dn: uid: Usernamedc=example, dc=com
cn: FirstnameLastname
sn: Lastname
telephoneNumber: Telephone number
Answer No. 3 & 4
Here Combined program to full fill the requirment of question number 3 and 4.
#!/bin/bash
# Leap year check
currentmonth=$(date +\"%B\")
month=$(date +%m)
leap=$(date +%Y)
M1=$01
M2=$02
M3=$03
M4=$04
M5=$05
M6=$06
M7=$07
M8=$08
M9=$09
M10=$10
M11=$11
M12=$12
M13=$13
echo \"The current month is $currentmonth\"
if [ $month -eq $M1 ]; then
echo \"$month has 31 days\"
elif [ $month -eq $M2 ]; then
echo \"$month has 28 days\"
elif [ $month -eq $M3 ]; then
echo \"$month has 31 days\"
elif [ $month -eq $M4 ]; then
echo \"$month has 30 days\"
elif [ $month -eq $M5 ]; then
echo \"$month has 31 days\"
elif [ $month -eq $M6 ]; then
echo \"$month has 30 days\"
elif [ $month -eq $M7 ]; then
echo \"$month has 31 days\"
elif [ $month -eq $M8 ]; then
echo \"$month has 31 days\"
elif [ $month -eq $M9 ]; then
echo \"$month has 30 days\"
elif [ $month -eq $M10 ]; then
echo \"$month has 31 days\"
elif [ $month -eq $M11 ]; then
echo \"$month has 30 days\"
elif [ $month -eq $M12 ]; then
echo \"$month has 31 days\"
else [ $[$leap % 400] -eq \"0\" ]; then
echo \"This is a leap year. February has 29 days\"
fi
Tested on a linux machine it working fine.


