Solved

Help need in unix scripting

Posted on 2014-02-03
10
362 Views
Last Modified: 2014-02-03
I wanted to create new application accounts on one of unix server through sctipt..

<?> capture thirdletter from the hostname.

username<?>su

example:
Servername: tedv1server (te<d>v1server)
username to create: usernamedsu (username<d>su)
0
Comment
Question by:surajindi4
  • 6
  • 4
10 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39830895
If your shell is bash or ksh93:

NAME="username"
HOST=$(hostname)
ACCOUNT=${NAME}${HOST:2:1}"su"
echo $ACCOUNT
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39830910
For other shells (e.g. older ksh implementations as in AIX or Solaris) which don't understand ${var:start:length} :

NAME="username"
HOST=`hostname`
CHAR=`expr substr $HOST 3 1`

ACCOUNT=$NAME$CHAR"su"
echo $ACCOUNT
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39830941
There is csh/tcsh, where we need a slightly different syntax:

set NAME = "username"
set HOST = `hostname`
set CHAR = `expr substr $HOST 3 1`

set ACCOUNT = $NAME$CHAR"su"
echo $ACCOUNT
0
 

Author Comment

by:surajindi4
ID: 39830988
Error: ACCOUNT=${NAME}${HOST:2:1}"ap": bad substitution


==
My script..

#!/bin/ksh
#
# what OS version are we running on?
OS=`uname -r`
# Make sure the HOST env variable is set
if [ -z "$HOST" ]; then
  HOST="`hostname`"
fi

PATH=/usr/bin:/sbin:/usr/local/bin:${PATH}
export PATH

display() {
  if [ "`which JS_display|awk '{print $1}'`" = "no" ];
  then
      echo "$*"
  else
      JS_display "$*"
  fi
}

NAME="testuser"
HOST=$(hostname)
ACCOUNT=${NAME}${HOST:2:1}"su"
echo $ACCOUNT

exit 0
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39831003
So your ksh is not ksh93 compatible. Try my second suggestion:

#!/bin/ksh
#
# what OS version are we running on?
OS=`uname -r`
# Make sure the HOST env variable is set
if [ -z "$HOST" ]; then
  HOST="`hostname`"
fi

PATH=/usr/bin:/sbin:/usr/local/bin:${PATH}
export PATH

display() {
  if [ "`which JS_display|awk '{print $1}'`" = "no" ];
  then
      echo "$*"
  else
      JS_display "$*"
  fi
}

NAME="testuser"
CHAR=`expr substr $HOST 3 1`
ACCOUNT=$NAME$CHAR"su"
echo $ACCOUNT


exit 0
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:surajindi4
ID: 39831015
expr: syntax error
testusersu


its not taking/adding 3rd char from hostname
0
 

Author Comment

by:surajindi4
ID: 39831027
it worked this way..

NAME="testuser"
HOST=`hostname`
CHAR=`hostname | cut -c3`
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39831042
Oh yes, it must be Solaris!

Use the UCB (BSD) version of expr:

NAME="testuser"
CHAR=`/usr/ucb/expr substr $HOST 3 1`
ACCOUNT=$NAME$CHAR"su"
echo $ACCOUNT
0
 

Author Comment

by:surajindi4
ID: 39831063
One more add-on req..

it third char "q" CHAR to change "a" is below code ok?

CHAR=`/usr/ucb/expr substr $HOST 3 1`
if
CHAR=[q]
then
CHAR=[a]
fi
ACCOUNT=$NAME$CHAR"ap"
echo $ACCOUNT
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39831130
if [ "$CHAR" = "q" ]; then CHAR="a"; fi
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now