ksh script not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script not working
# 1  
Old 06-10-2013
ksh script not working

Here's the script:

Code:
#!/usr/bin/ksh
Date=`date +%m%d%y`
CDate=`date`
FileName=cintas_hosts_and_users.$Date
echo $CDate >> $FileName
#echo $FileName
for host in `cat /collect/itthomp/cintas_hostnames.dat` 
do
echo $host  >> $FileName
ssh $host "awk -v Fname=$FileName -F: '{if($1 != "root" && $1 != "daemon" && $1 != "bin" && $1 != "sys" && $1 != "adm" && $1 != "uucp" && $1 != "guest" && $1 != "nobody" && $1 != "lpd" && $1 != "lp" && $1 != "invscout" && $1 != "snapp" && $1 != "ipsec" && $1 != "nuucp" && $1 != "pconsole" && $1 != "sshd" && $1 != "idsldap" && $1 != "sapadm")print $1 "\t" $5 >> Fname}' /etc/passwd"
done

Here's the error I get:

Code:
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
Syntax Error The source line is 1.
The error context is
                {if( >>>  != <<<

I'm not very experienced with scripting and this is my first attempt at using awk within a ksh script. Any help please would be appreciated. Thanks...

Last edited by Scrutinizer; 06-10-2013 at 07:41 PM.. Reason: code tags
# 2  
Old 06-10-2013
The entire awk script is enclosed by double quotes. $1 is expanded by the local shell before it even executes ssh.

Regards and welcome to the forum,
Alister
# 3  
Old 06-10-2013
Is there any way I can fix this or do I need to take another approach to the solution?

Thanks for the reply...
# 4  
Old 06-10-2013
As alister mentioned the variables got expanded by the shell even before it executes ssh.

To avoid this happening, you have to escape them as highlighted below.

You can also simplify your awk program using arrays:
Code:
ssh -q $host "
awk -F':' '
        BEGIN {
                n = split ( \"root:daemon:bin:sys:adm:uucp:guest:nobody:lpd:lp:invscout:snapp:ipsec:nuucp:pconsole:sshd:idsldap:sapadm\", A )
                for ( i = 1; i <= n; i++ )
                        U[A[i]]
        }
        !(\$1 in U) {
                print \$1, \$5
        }
' OFS='\t' /etc/passwd"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Linux ksh script not working in crontab

I am Not sure why following script is not capturing the counts only when using crontab !! when I run it fromt he command line it is fine ! what is missing here ! #!/usr/bin/ksh host=`uname -n` tdate=`date` userid='dbid/password' totalevents=`sqlplus -s $userid << - set timing off ... (1 Reply)
Discussion started by: mrn6430
1 Replies

2. UNIX for Dummies Questions & Answers

Cd and mv in a tar ksh script not working

hi forums, can you help me on how to make cd and mv code work in this script? #!/bin/ksh #i got it from: https://www.unix.com/shell-programming-and-scripting/107612-how-tar-compress-remove-files-older-than-two-days.html MAIN_DIR=/home/user/pumela find ${MAIN_DIR} *.txt -mtime -3> FILE_LIST... (7 Replies)
Discussion started by: phumaree
7 Replies

3. Shell Programming and Scripting

Script not working with ksh

Hi I am not able to execute a script with ksh. Its not giving any error too. I have tried with absolute path /usr/bin/ksh test.sh . Also checked path variable and all looks fine It runs fine with sh. OS is solaris 10. (9 Replies)
Discussion started by: ningy
9 Replies

4. Shell Programming and Scripting

Working with FOR in ksh 88

Hi, I tried the following but giving me all the files in the directory , Where i need the files which are assigned to variable like below #!/bin/ksh Src_Dir="/home/etc" file_nm ="ab.temp" for File in `ls $Src_Dir/$file_nm*` do File=`basename $File` echo $File ... (2 Replies)
Discussion started by: smile689
2 Replies

5. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

6. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for... (5 Replies)
Discussion started by: thibodc
5 Replies

7. Shell Programming and Scripting

ksh script not working if triggered by scheduler

I have a script that works well if i execute manually using informix user. However, it does not execute properly if triggered using the scheduler (ESP). This is the partial part where it doesn't work. i added some tracing but i can't figure it out. #!/bin/ksh let db_is_up=0... (6 Replies)
Discussion started by: tungaw2004
6 Replies

8. Shell Programming and Scripting

mmin not working in ksh

We have created a unix shell script to read a datafiles from specific input directory in Unix. Users will be copying datafiles to the same input unix directoty. During Testing we observed Unix Shell Script also read the incomplete datafiles which is still copying by the users. As per requirement... (1 Reply)
Discussion started by: Kumari Reshma
1 Replies

9. Shell Programming and Scripting

host cp in ksh script not working

The following script is designed to backup the oracle control file to the trace directory and then copy (the trace file that was created by the backup command) and rename that file(to a .sql) to a backup disk. When I run the script from sqlplus as sysdba everything works but when I execute from... (1 Reply)
Discussion started by: tparker123
1 Replies

10. UNIX for Advanced & Expert Users

formatting textfile inside ksh script using awk not working

I cannot seem to get this text file to format. Its as if the awk statement is being treated as a simple cat command. I manned awk and it was very confusing. I viewed previous posts on this board and I got the same results as with the the awk command statement shown here. Please help. ... (6 Replies)
Discussion started by: tekline
6 Replies
Login or Register to Ask a Question