Operating System - HP-UX
1832990 Members
2292 Online
110048 Solutions
New Discussion

Newbie needs help with scripting

 
Karri Kangas
Occasional Contributor

Newbie needs help with scripting

Hello, I am quite unexperienced in Unix scripting, so I have few basic questions. I have a script which reads the current month from the system clock and then feeds the number of previous month as a parameter to a program. However, it doesn't work. I put my script here and I hope you could help me to solve those problems. OS is HP-UX 11.0 and I use sh-shell.

[code]
kk=`date +'%Om'`

if [$kk -eq 1 ]; then
ed=12
else
ed=$((kk-1))
fi
su user1
1resu
. /mnt01/app/admin/pro2000/setup/dbconfig.invoice
. /mnt01/app/invoice/bin/injorabatch -i $ed
echo $ed
[/code]

Now, when I execute this script as "user2", it should switch the user and then execute those 2 programs. However, when I run program as user2 (its name is test) it reacts as follows (ie. there is at least 2 problems in my script):

$ . test
sh: [11: not found.
Password:

If I log in as user1 and run the program, it does as follows:

hp3: $ . test
test: bad pattern: [11 [7]
test: command not found: kk-1 [7]
Password:
hp3: $

I would appreciate any and all advices you may have. Thank you.
5 REPLIES 5
Sridhar Bhaskarla
Honored Contributor

Re: Newbie needs help with scripting

Hi,

First thing is not to keep the script names the same as commands.. So, rename the script as mytest.sh or something other than 'test'.

Secondly, there should be a space in your if statement. It should have read

if [ $kk -eq 1 ]; then

There is a space between [ and $kk in the above if it is not preserved after the posting.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Karri Kangas
Occasional Contributor

Re: Newbie needs help with scripting

Thank you Sri. The space in the place you mentioned definately helped. I also changed the name of my test program. Now it went without problems until it asked the password (I am trying to make script so that it doesn't ask password, but reads it from that same file). I provided it and program ended without further warnings. However, it did not execute at least the latter program (injorabatch). Here is what it did:

$ . mytest.sh
Password:
hp3: $

Next step are following:
1. script doesn't ask password again.
2. Script executes injorabatch program.
Sridhar Bhaskarla
Honored Contributor

Re: Newbie needs help with scripting

Hi,

Not easy if you are doing it as an ordinary user as 'su' will prompt for the password. There are couple of options for you.

1. Use 'sudo' instead of 'su'. You can specify "NOPASSWD" option in sudo configuration file 'sudoers' so it won't ask for password.
2. Write an expect or perl program that will make the interactive su to non-interactive.

Option 1 is easy. But you will need to understand the format of 'sudoers' which is not difficult either. Get it from

http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/sudo-1.6.7p5/

Once it is installed, run the command 'visudo' and add the lines

Cmnd_Alias CMDS = . /mnt01/app/invoice/bin/mybatchjob

login1 ALL = (user1) NOPASSWD: CMDS

The above will enable the login 'login1' to run the program mybatchjob as 'user1' without having to specify the password.

Add all your commands in mybatchjob.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Karri Kangas
Occasional Contributor

Re: Newbie needs help with scripting

Okay. Sudo is not a standard unix command but additional program. Well, I can also run my program as user2 too, it is not necessary.

Actually, I managed to make it work. I used chmod so that every user can execute that program. Then I executed as user2, and it went through. Problem solved.

Thank you very much of your time and effort, Sri. :)

-Karri Kangas
Karri Kangas
Occasional Contributor

Re: Newbie needs help with scripting

I mean that I executed it as user1 (there should be edit possibility for answers).