- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2005 11:10 PM
11-06-2005 11:10 PM
while [ x$1 != "x" ]
I have not done scripting before and do not understand the arguments inside the while loop (brackets) as above.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2005 11:15 PM
11-06-2005 11:15 PM
Re: scripting
$1=varibale defined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2005 11:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2005 11:55 PM
11-06-2005 11:55 PM
Re: scripting
it is a kind of standard trick for detecting if a variable has been set.
In this case it checks if the script in which it is used has been activated with a parameter(=$1) or not: if $1 is non-exisiting, then x$1 is equal to x
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2005 11:58 PM
11-06-2005 11:58 PM
Re: scripting
The expression will evaluate to true if "$1" isn't empty, otherwise it will evaluate to false.
You can easily see this by doing:
cat /tmp/debug
#!/usr/bin/sh
if [ x$1 != "x" ]; then
echo ok
fi
# sh -x /tmp/debug #...no arguments
# sh -x /tmp/debug arg #...any argument
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 12:02 AM
11-07-2005 12:02 AM
Re: scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 01:24 AM
11-07-2005 01:24 AM
Re: scripting
The script is as follows:
echo "Please enter the User's Surname and first TWO Christian Names only, "
echo "separated by spaces. Please use only lower case. CTRL-C to exit."
echo
echo "For example \"jones derick richard\" for Derick Richard Jones."
echo
echo " User's name (lower case only): \c"
read NAME
unset CHR1 CHR2 SURNAME EXIST USERCHECK
COUNT=0
set $NAME
while [ x$1 != "x" ]
do
COUNT=`expr $COUNT + 1`
case $COUNT in
1) SURNAME=$1 ;;
2) CHR1=$1 ;;
3) CHR2=$1 ;;
esac
shift 1
done
INIT1=`echo $CHR1 | cut -c1`
INIT2=`echo $CHR2 | cut -c1`
if [ x$INIT2 = "x" ]
then
INIT2=a
fi
SNAME=`echo $SURNAME | cut -c1-5`
while [ ${#SNAME} -lt 5 ]
do
SNAME=$SNAME"a"
done
UNAME=`echo $SNAME$INIT1$INIT2`
USERCHECK=`echo "$UNAME:"`
EXIST=`grep $USERCHECK /etc/passwd | awk -F: {'print $1'}`
if [ x$EXIST != "x" ]
then
COUNT=1
for i in $EXIST
do
COUNT=`expr $COUNT + 1`
done
UNAME=`echo $UNAME$COUNT`
fi
HOMEDIR="/home/gvts"
if [ $INIT2 = a ]
then
COMMENTS="STAFF $CHR1 $SURNAME"
else
COMMENTS="STAFF $CHR1 $CHR2 $SURNAME"
fi
clear
echo
echo " GVTS User creation utility"
echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
echo "The user account will be created as follows:"
echo
echo " User ID: $UNAME"
# echo " User ID2: $UNAME"t""
echo " Home directory: $HOMEDIR"
echo " Comments: $COMMENTS"
sleep 3
echo "useradd -g gvts -s /bin/false -d $HOMEDIR -c \"$COMMENTS\" $UNAME" > u
# echo "useradd -g gvts -s /bin/false -d $HOMEDIR -c \"$COMMENTS\" $UNAME"t"" >> u
echo "echo "Enter password for $UNAME"" >> u
echo "passwd $UNAME" >> u
# echo "echo "Enter password for $UNAME"t""" >> u
# echo "passwd $UNAME"t"" >> u
sh u
rm u
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 01:46 AM
11-07-2005 01:46 AM
Re: scripting
x is simply a random letter in this case.
It is not important which letter is used, in the script however, x is used:
while [ x$1 != "x" ]
and
if [ x$INIT2 = "x" ]
you could as well have used:
while [ y$1 != "y" ]
and
if [ z$INIT2 = "z" ]
the idea is simply to test if the accompanying variable, $1 and $INIT2, respectively, have been set, i.e. do they have a value or are they equal to nothing.
If equal to nothing, the letter chosen is equal to the letter chosen!
In you script the idea is to repeat the question until the user supplies input, which then is considered $1 by the script.
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 02:24 AM
11-07-2005 02:24 AM
Re: scripting
The statement while[x$1 != "x" ] breaks down like this
$1 is the command line parameter. As stated above if your script is run.sh the command line parameters would be Smith and William and the first ($1) would be Smith
#run.sh Smith William
if you just execute the script with no parameters $1 is a null and the test program ([) will return an error like value expected. However, if instead of testing for just $1, you append something to the front of $1, for instance x, then you get x$1 which translates to xSmith in the example above but only to x if you provide no parameters. Now your have something to test with that will not return the value expected, sense it will always have at least the value of x.
thatâ s why the test is for "x" and not "" if no parameter is provided.
In the case of your script, what its doing is, if there is no parameter (in this case names) sent then it asks for them in the script, if there is a parameter, (the name) it has the name and doesnâ t need to prompt for it inside the script.
Hope that helps
H
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 02:56 AM
11-07-2005 02:56 AM
Re: scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 03:12 AM
11-07-2005 03:12 AM
Re: scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 04:38 AM
11-07-2005 04:38 AM
Re: scripting
echo
echo " User's name (lower case only): \c"
read NAME
unset CHR1 CHR2 SURNAME EXIST USERCHECK
COUNT=0
set $NAME
set name as first parameter (aka $1)
while [ x$1 != "x" ]
do
COUNT=`expr $COUNT + 1`
case $COUNT in
1) SURNAME=$1 ;;
2) CHR1=$1 ;;
3) CHR2=$1 ;;
esac
shift 1
done
assign NAME to SURNAME, then CHR1 then CHR2
INIT1=`echo $CHR1 | cut -c1`
INIT2=`echo $CHR2 | cut -c1`
make INIT1 initial of CHR1, INIT2 liekwise
if [ x$INIT2 = "x" ]
then
INIT2=a
fi
if no INIT2, then assing aritrary a to INIT2
SNAME=`echo $SURNAME | cut -c1-5`
SNAME is first 5 letter of surname ...
while [ ${#SNAME} -lt 5 ]
do
SNAME=$SNAME"a"
done
...padded with "a"
UNAME=`echo $SNAME$INIT1$INIT2`
now Joe Doe gave UNAME=doeaaja
and Joe Mike Smithson gave smithjm
wow this gave a nice 7 letter login name !!!
USERCHECK=`echo "$UNAME:"`
EXIST=`grep $USERCHECK /etc/passwd | awk -F: {'print $1'}`
what about if name is alread on :etc/passwd ?
if [ x$EXIST != "x" ]
then
COUNT=1
for i in $EXIST
do
COUNT=`expr $COUNT + 1`
done
UNAME=`echo $UNAME$COUNT`
fi
count number of occurence (and prolly a syntax error here, one should write
if [ "x$EXIST" != "x" ]
instead of
if [ x$EXIST != "x" ]
besides, if there is more than 9 matches, resulting name might be 9 letter long, a problem on sone unixes ...)
HOMEDIR="/home/gvts"
if [ $INIT2 = a ]
then
COMMENTS="STAFF $CHR1 $SURNAME"
else
COMMENTS="STAFF $CHR1 $CHR2 $SURNAME"
fi
prepare Gcos variable in /etc/passwd (5th filed) with real name.
clear
echo
echo " GVTS User creation utility"
echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
echo "The user account will be created as follows:"
echo
echo " User ID: $UNAME"
# echo " User ID2: $UNAME"t""
echo " Home directory: $HOMEDIR"
echo " Comments: $COMMENTS"
sleep 3
tell us about the creation of unix account (by the way where is HOMEDIR defined ?)
echo "useradd -g gvts -s /bin/false -d $HOMEDIR -c \"$COMMENTS\" $UNAME" > u
# echo "useradd -g gvts -s /bin/false -d $HOMEDIR -c \"$COMMENTS\" $UNAME"t"" >> u
echo "echo "Enter password for $UNAME"" >> u
echo "passwd $UNAME" >> u
# echo "echo "Enter password for $UNAME"t""" >> u
# echo "passwd $UNAME"t"" >> u
sh u
rm u
done
where that's done comming from ? did you copy paste it all ?
--
are you having trouble with this script ?
does it run well ?
Jean-Yves Picard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 06:28 AM
11-07-2005 06:28 AM
Re: scripting
To add a little detail to the part where the user's input is read in:
After presenting the instructions about how the user is supposed to enter his input:
read NAME
...assigns all input made by the user into a single variable: if the user wrote:
skov ib bo
these three names will then all be kept in NAME
set $NAME
...has the effect that the content of NAME is broken up into space separated items, i.e. positional parameters, that can be addressed individually; now $1 contains the word skov, $2 contains the word ib, and $3 the word bo.
This is important to understand as the script seems to lose interes in the NAME variable!
The script now chews its way through these positional parameters:
while [ x$1 != "x" ]
...until positional parameter no. 1 is "empty" do this;
COUNT=`expr $COUNT + 1`
...add one to COUNT (first time COUNT is "1")
case $COUNT in
...if COUNT is 1, 2, or 3, do the requested action for the value in question:
1) SURNAME=$1 ;;
...COUNT is 1, assign the current $1 value, skov, to the variable SURNAME
...the script can select only a single option of the case-structure; as "1" was used the script then jumps to the end of the case-structure:
esac
...at this moment there are still three positional parameters:
skov ib bo
...but this is now changed by
shift 1
...which removes the previous $1 value, skov, leaving only
ib bo
...Notice that $1 is now ib
...Then the loop is evaluated again:
while [ x$1 != "x" ]
...$1 is not empty, as it now contains
ib
...COUNT is incremented by one
COUNT=`expr $COUNT + 1`
...And is now 2
...Therefore the
case $COUNT in
...now enters
2) CHR1=$1 ;;
...Which assigns ib to CHAR1
Shift 1
...Changes $1 to
bo
...and the third time we enter the loop bo is assigned to CHR2
...after the third
shift 1
there is no longer any $1 and the evaluation
while [ x$1 != "x" ]
...is now true, e.g.
x is equal to x and the script jumps to the first line after done..
A long explanation for a small part, but the way NAME is handled is a little tricky.
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2005 01:58 PM
11-07-2005 01:58 PM
Re: scripting
[ x$1 != "x" ]
is now replaced with the more readable technique:
[ "$1" != "" ]
By placing the parameter ($1 in this case) within double quotes, the test for a missing or null value makes much more sense. If $1 is null or a zero-length string the test (the square brackets) will be correctly evaluated.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2005 03:36 AM
11-08-2005 03:36 AM
Re: scripting
This construct is quite old. See Bill's answer for the current correct syntax.
Since this is an interactive script, it really needs to do a lot of error-checking. Typically, when I write an interactive script, I'll test all the expected parameters with something like this:
if [ -z $1 ]
then
echo "Missing parameter."
exit 1
fi
The -z means if the value of the string $1 is null, evaluate to true. The test(1m) man page will give you a much more detailed description.
-or-
if [ $# -ne 3 ]
then
echo "Missing parameters."
exit 1
fi
The $# is the number (#) of parameters from the command line. This code just says that if the number of parameters is not equal to 3 , then stop.
You should always let the user know when something is wrong and stops the script. This can be extremely important if the scripts does anything 'dangerous', like removing files.
Regards,
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2005 10:28 AM
11-08-2005 10:28 AM
Re: scripting
If you are using any contempory Bourne shell compatible like ksh, bash, or HP-UX sh,
the the odd idiom isn't needed and obsolete
because the [[ ]] test command prevents word splitting and pathname globbing but does all the variable, parameter, command and arithmetic expansions.
This lets you write more tangibly
while [[ $1 != "" ]]; do
or better yet
while [[ -n $1 ]]; do
or even as arithmetic expression,
but probably more obfuscated
while (( ${#1} )); do
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2005 09:27 PM
11-10-2005 09:27 PM