Solved

Help need in unix scripting

Posted on 2014-02-03
10
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux performance issues with 100% swap usage, etc 15 105
Remote Change Dates on AIX Automation 7 87
Image not there 4 76
centos commands 6 118
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
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.
Suggested Courses

751 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