LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-21-2014, 06:28 AM   #1
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Rep: Reputation: Disabled
About the shell script "/etc/profile"


Code:
# /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
 
Old 07-21-2014, 07:39 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
$2 is "the second passing argument to the script"

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:
  1. Bash Scripting Guide for Beginners
  2. Advanced Bash Scripting Guide
  3. blog entry on bash programming

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.
 
Old 07-21-2014, 07:44 AM   #3
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
$2 is "the second passing argument to the script"

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:
  1. Bash Scripting Guide for Beginners
  2. Advanced Bash Scripting Guide
  3. blog entry on bash programming

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"
 
Old 07-21-2014, 07:58 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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:
  1. 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
  2. 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
  3. 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.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
[SOLVED] Shell scripting, difference of "source script.sh" and "./script.sh" Squall90 Programming 4 07-31-2010 04:40 PM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
converting shell script from "dialog" to "xdialog" kushalkoolwal Programming 1 02-25-2008 08:40 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:12 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