LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2006, 04:07 AM   #1
kb100
Member
 
Registered: Aug 2006
Posts: 43

Rep: Reputation: 15
simple script help!! Please


Hi there. Does anyone know how to
a) test a command line argument of a user's username
b) test to make sure that a command line argument was provided.

thanks
 
Old 08-23-2006, 05:48 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
- You obviously didn't read any of the docs everyone mentioned to you.
- You already know how to test a condition from your other threads.

For username you can use the standard "$LOGNAME" and the amount of commandline arguments is "$#". Now please post whatever you got already. From now on that should be the default thing for you to do since you are learning scripting.
 
Old 08-24-2006, 02:54 AM   #3
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
I just dont understand what it means by command line argument. So what does the user have to input?
 
Old 08-24-2006, 07:29 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I can try and be patient, and 'm willing to explain things but if you can't even read a post and supply what's asked for this isn't going to be very efficient. With what I posted you could have made a script to test things out, like:
Code:
#!/bin/sh
set -e
echo "The amount of commandline args is $#"
echo "They are \"$@\""
n=0; for arg in $@; do
 if [ "$arg" = "$LOGNAME" ]; then
  echo "Number $n is logname"
 fi
 let n=${n}+1; shift $n;
done
exit 0
then name it say "cli_test", give it a few arguments on the commandline and wedge in your local username, like:
"sh cli_test abc 123 $LOGNAME 456". See? Now run it as "sh -x cli_test abc 123 $LOGNAME 456" and you see how the variables get filled in. I would suggest you next first read these two Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
 
Old 08-25-2006, 03:35 AM   #5
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Hi there thanks for your reply. I think the main problem is that i dont understand what commandline means. If you take a commandline of a user's username what is that exactly asking the script to do (In lamens terms!!) because i am just a beginner attempting certain questions just to help me. I have been through the tutorials but it is a lot to take in especially as there are not that many examples.

thanks
 
Old 08-25-2006, 05:08 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by kb100
Hi there thanks for your reply. I think the main problem is that i dont understand what commandline means.
You're doing BASH scripting and you don't know what the command line is?? The command line is well, the place where you enter commands to be run..

Quote:
If you take a commandline of a user's username what is that exactly asking the script to do
That doesn't actually make any sense :/.
 
Old 08-25-2006, 05:37 AM   #7
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
the command line in bash is where you type-in your commands like mv, cp, ls etc. now if you type-in additional parameters extending the command, the additional parameters are called command-line parameters. for example, let's say i have a script called "help" which i use to get help about different commands from the system. so when i type "./help cp" in the command-line it prints out manpages of cp for me. you see here the "./help" is the actual command and the "cp" following the command is a command-line argument.

now from unspawn's script,
Code:
1 #!/bin/sh
2 set -e
3 echo "The amount of commandline args is $#"
4 echo "They are \"$@\""
5 n=0; for arg in $@; do
6  if [ "$arg" = "$LOGNAME" ]; then
7   echo "Number $n is logname"
8  fi
9  let n=${n}+1; shift $n;
10 done
11exit 0
the 3rd line prints out the total number of command-line arguments you passed to script when you ran it. if we call this script "test" and you type "./test root usex", this line will print
Quote:
The amount of commandline args is 2

Last edited by prozac; 08-25-2006 at 06:05 AM.
 
Old 08-25-2006, 06:12 AM   #8
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
thanks PROZAC for your reply. If other people are asking me why i dont know what a command line is well i wouldn be asking would i? Thanks PROZAC.
 
Old 08-25-2006, 07:42 AM   #9
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
How do you run your scripts if you're not using the command line then? They're Bash scripts so you need to run them in the shell..
 
Old 08-25-2006, 08:19 AM   #10
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
how do you print all the processes "root" is running?

thanks
 
Old 08-25-2006, 09:11 AM   #11
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
Code:
ps -U root -u root u
 
Old 08-26-2006, 03:13 AM   #12
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Hi there thanks for reply. Does anyone know how to print a users home directory path?

thanks
 
Old 08-26-2006, 03:39 AM   #13
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
if you are logged in as that user, you can do a "echo $HOME" in the shell to print that user's home directory, or you can look-it-up in the file /etc/passwd.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Need help with a simple script shell script WindowBreaker Linux - Software 2 12-15-2005 12:45 PM
Help on simple script granny Programming 2 02-11-2004 04:02 PM
Simple Script fiod Linux - General 2 09-08-2003 03:03 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration