Solved

Help need in unix scripting

Posted on 2014-02-03
10
359 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:surajindi4
Comment Utility
expr: syntax error
testusersu


its not taking/adding 3rd char from hostname
0
 

Author Comment

by:surajindi4
Comment Utility
it worked this way..

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

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
if [ "$CHAR" = "q" ]; then CHAR="a"; fi
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now