Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# ksh workaround
if [ -z "$EUID" -a -x /usr/bin/id ]; then
EUID=`id -u`
UID=`id -ru`
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
if [ -x /usr/bin/id ]; then
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
HOSTNAME=`/bin/hostname`
HISTSIZE=1000
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
unset i
unset pathmunge
as above :
i can not understand the variable $2
i don't understand where it comes from and how it gets
assignment
i am just beginning to learn shell script and a newbie
please give me some guide
the following is my script i write the $2
but it doesn't work
Code:
#!/bin/bash
echo "Please input a path to test whether it exists in PATH"
read -p "Please input a path: " path1
read -p "Please input a path: " path2
pathmunge() {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)";then
echo " no exist! and will add it to PATH. "
if [ "$2" = "after" ]; then
echo "$2=after"
PATH=$PATH:$1
else
echo "$2=before"
PATH=$1:$PATH
fi
fi
}
pathmunge $path1
pathmunge $path2
every time the $2 is "".
Last edited by onebuck; 07-21-2014 at 08:09 AM.
Reason: clean up by using Vbcode tags
If you have a script, say the script name is my-script.sh. Therefore when you run it you can either give it arguments, or not. An example of my-script.sh being run with no arguments:
Code:
./my-script.sh
An example of my-script.sh being run with two arguments:
Code:
./my-script.sh 1 50
In the first example, there are no arguments, in the second example $1 is "1" and $2 is "50".
Some suggested reading and some suggestions for posting:
When you post code, output from your commands, or examples, please use [code][/code] markers, the [code][/code] gets interpreted to cause the encapsulations showing as above.
Since your post was lengthy (not a problem if it's involved) but because it was a mixture of code and comments/questions, I was able to go to the bottom and move up a bit to determine that you misunderstood script passing arguments, but there could be other questions which I missed because of the organization. If you're able to organize your questions better, you'll get the answers you're looking for faster. Another suggestion is to ask limited questions to get answers incrementally.
If you have a script, say the script name is my-script.sh. Therefore when you run it you can either give it arguments, or not. An example of my-script.sh being run with no arguments:
Code:
./my-script.sh
An example of my-script.sh being run with two arguments:
Code:
./my-script.sh 1 50
In the first example, there are no arguments, in the second example $1 is "1" and $2 is "50".
Some suggested reading and some suggestions for posting:
When you post code, output from your commands, or examples, please use [code][/code] markers, the [code][/code] gets interpreted to cause the encapsulations showing as above.
Since your post was lengthy (not a problem if it's involved) but because it was a mixture of code and comments/questions, I was able to go to the bottom and move up a bit to determine that you misunderstood script passing arguments, but there could be other questions which I missed because of the organization. If you're able to organize your questions better, you'll get the answers you're looking for faster. Another suggestion is to ask limited questions to get answers incrementally.
i think in this situation ,$2 should be the second passing argument to the function "pathmunge"
So what you're saying is that you're just not following the script that you happen to be reading?
Some suggestions on that are:
Since this is a system script, it's a great, but bad example because you're not running it from the shell, instead it's running as part of a login or boot sequence (login actually in this case) and hence any stdout/stderr information is not being shown. Perhaps practice instead writing scripts and debugging them or with scripts which you run from your shell first so you can modify them and see stdout/stderr as the script runs
You can get debug for that script by echo'ing things to a file, for instance you can do like
Code:
"echo "The second argument is $2" >> /home/<my-login>/profile-debug.txt;
Put that within the script at an appropriate point to give yourself debug, and similarly you can get a lot of debug by adding similar statements
Review the blog to see some debug suggestions, but note that the "set -xv" one works best when you're running the script from the command line, because again if a script is run by the system, all the stdout and stderr information goes ... somewhere. Perhaps someone else can offer a statement as to how you can view that, if at all. My take on it is that I'd run the script from the command line to enforce getting the information visible to me. There are also ways when the script is run to redirect stdout and stderr, but here you are not controlling how or when the script is run.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.