Solved

Help need in unix scripting

Posted on 2014-02-03
10
370 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

713 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