ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
- 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.
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.