nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Alphabetical file name and list logged in user script

This is a discussion on Alphabetical file name and list logged in user script within the Shell scripting forums, part of the Development/Scripting category; Hi, i couldnt figure out how to write this script. can any one help me ? please responce asap problem ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-08-2007, 04:43 PM
Junior Member
User
 
Join Date: Aug 2007
OS: Debian
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
oronno is on a distinguished road
Thumbs down Alphabetical file name and list logged in user script

Hi, i couldnt figure out how to write this script. can any one help me ? please responce asap

problem 1: Write a shell script (/bin/sh) to write the names of the first (alphabetically) five files of the current directory to a new file called ‘zzz’.

problem 2: Write a shell script (/bin/sh) called 'loggedOn' which is passed two usernames as command line parameters. Using the ‘who’ command, the script should see whether both users are logged on and, if so, should indicate which of them is logged onto the system the most times. If only one or neither is logged on, an error message should be written.
Reply With Quote
  #2 (permalink)  
Old 13-08-2007, 01:27 AM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 708
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
Default

Quote:
problem 1: Write a shell script (/bin/sh) to write the names of the first (alphabetically) five files of the current directory to a new file called ‘zzz’.
By default ls command sort out names under Linux:
Code:
#!/bin/sh
ls | head -5
Quote:
problem 2: Write a shell script (/bin/sh) called 'loggedOn' which is passed two usernames as command line parameters. Using the ‘who’ command, the script should see whether both users are logged on and, if so, should indicate which of them is logged onto the system the most times. If only one or neither is logged on, an error message should be written.
Script:
Code:
#!/bin/bash
USR1=$1
USR2=$2
u1times=0
u2times=0
if [ $# -ne 2 ]; then
 echo "Syntax: $0 user1 user2"
 exit 1
fi
who | egrep -w $USR1 > /dev/null
if [ $? -eq 0 ]; then
        u1times=$(last | egrep -w $USR1)
else
        echo "Error $USR1 not logged in"
        exit 1
fi

who | egrep -w $USR2 > /dev/null
if [ $? -eq 0 ]; then
        u2times=$(last | egrep -w $USR2)
else
        echo "Error $USR2 not logged in"
        exit 1
fi
if [ $u1times -gt $u2times ]; then
        echo "$USR1 logged most of the time"
else
        echo "$USR2 logged most of the time"
fi

Hope you are able to explain it to your tutor
__________________
Rocky Jr.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
  #3 (permalink)  
Old 13-08-2007, 06:38 AM
Junior Member
User
 
Join Date: Aug 2007
OS: Debian
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
oronno is on a distinguished road
Default to rockdalinux

thanks for your help buddy. but the first problem u solved is not clear for me. because there is something i need to make, such as creat a new file and u didnt complete the whole thing . can you please give me full script for problem 1 ? thanks
Reply With Quote
  #4 (permalink)  
Old 15-08-2007, 11:40 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Code:
#!/bin/sh
ls | head -5 > zzz
there I fixed for you
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 16-08-2007, 05:08 PM
Junior Member
User
 
Join Date: Jul 2007
Location: Cedar Falls, IA
OS: Fedora
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
A.L.E.X is on a distinguished road
Default

don't you guys think that

Code:
ls | egrep -v '^[0-9]' | head -5 > zzz
will be better solution ? I really don't know ... "alphabetically" confuse me
Reply With Quote
  #6 (permalink)  
Old 22-08-2007, 12:10 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
Default

Quote:
Originally Posted by A.L.E.X View Post
don't you guys think that

Code:
ls | egrep -v '^[0-9]' | head -5 > zzz
will be better solution ? I really don't know ... "alphabetically" confuse me
Yes alphabetically means ignore out digits .. I guess your solution is correct
__________________
May the force with you!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Getting the list of directory names into a file hiddenrider007 Shell scripting 2 09-04-2008 09:59 AM
AIX /etc/security/user like file for Linux ftengcheng Getting started tutorials 4 25-03-2008 01:28 AM
script for mailing to user the o/p of the script ricc Shell scripting 3 11-07-2007 04:59 AM
list every user's crontab script marinm Shell scripting 5 19-07-2005 11:24 PM
shell script for finding users not logged on for last 10 day ganes Shell scripting 4 06-07-2005 11:01 AM


All times are GMT +5.5. The time now is 06:13 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38