Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help need in unix scripting

Posted on 2014-02-03
10
Medium Priority
?
380 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 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

972 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