How to run "finger" command in an if statement?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run "finger" command in an if statement?
# 1  
Old 08-04-2016
Display How to run "finger" command in an if statement?

I have been trying to run the finger command in a if statement but its giving me a bunch of errors.
Code:
gidlistTemp="g274gG;g2759C;g28320;g2885G;g2A276;g23338;g2A5h5;g2A307"
for i in $(echo $gidlistTemp| tr ';' ' \n')
do
        tst=(finger $i | wc -l)
        if [$tst != 0]
        then
                gidlist=$gidlist$i
        fi
done

How do I fix this?
# 2  
Old 08-04-2016
Code:
tst=$(finger $i | wc -l)

# 3  
Old 08-04-2016
Doesn't work, the $ makes in front of finger makes it think that finger is a variable.
# 4  
Old 08-04-2016
No, it sees the $( ... ). A Posix-compatible shell treats it like the traditional ` ... `
Further, make sure there a spaces around the [ ]
Code:
 if [ $tst != 0 ]

# 5  
Old 08-04-2016
Quote:
Originally Posted by ajetangay
Doesn't work, the $ makes in front of finger makes it think that finger is a variable.
Expanding a bit on what MadeInGermany already said... the code rdrtx1 suggested:
Code:
tst=$(finger $i | wc -l)

does not have a dollar sign in front of finger; it has a dollar sign in front of an opening parenthesis. What rdrtx1 suggested was to execute the pipeline finger $i | wc -l and store the output written to its standard output in the variable named tst.

Your code:
Code:
tst=(finger $i | wc -l)

(making the wild assumption that you are using a shell that has array variables) attempts to make tst an array of values that arise from splitting the string finger current_value_of_variable_i | wc -l but gets a syntax error because a pipeline has higher precedence than assigning an array to a variable. And, if you get past that syntax error (and ignore the probable bug in the command substitution:
Code:
$(echo $gidlistTemp| tr ';' ' \n')

which will be treated exactly the same as the common substitution:
Code:
$(echo $gidlistTemp| tr ';' ' ')

when I'm not sure whether you wanted to alternate between <space> and <newline> or just want to replace all occurrences of <semicolon> with <space> or to replace all occurrences of <semicolon> with <newline>), we then get to the syntax errors in your if statement:
Code:
        if [$tst != 0]

which (assuming that you are using a shell based on Bourne shell syntax) requires a space between [ and $tst and between 0 and ] and for a numeric comparison should use -ne instead of !=. Then we also have the problem that we do not know what system you're using and what output its version of finger produces when a user is looked in and when a user is not logged in. On OS X (using a BSD based finger utility) finger produces five lines of output for a user that is not logged in and 5 + n lines of output for a user that is currently logged in n times.

So, if we remove all of your syntax errors, it appears that what you are trying to do is remove all of the <semicolon> characters from the string stored in the variable gidlistTemp and store the result of that conversion in the variable named gidlist.

But, since you have never told us what you are trying to do, what operating system you're using, what shell you're using, nor the exact "bunch of errors" your script is producing; we are left to make wild guesses at how to correct your script. If you would be willing to share all of this information with us, we might be able to help you correct your script to do what you want to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to run root level command , if user has "su -" permission in sudoers provided?

I am looking t run root level command on multiple servers, but all servers have only "su - " permission available in sudoers. please help me if any way that I can run command using help of "su -" My script for hosts in `cat hosts.txt`; do echo "###########################Server Name-... (5 Replies)
Discussion started by: yash_message
5 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

What is the use of "finger" command & how to use it to kill the online processes ?

Hi there, I am eager to know what exactly is the use of "finger" command & how to use it to kill the online processes ? :b: (1 Reply)
Discussion started by: abhijitpaul0212
1 Replies

4. Shell Programming and Scripting

Source command returns error when it strikes conditional statement "ifeq"

Hello All, I am running source command on my project configuration file app.cfg which has conditional statements with make file systax E.g ifeq ($(APP_CMP_DIR),trunk). When I source this file it throws error: syntax error near unexpected token... (1 Reply)
Discussion started by: anand.shah
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. AIX

After run ps , uptime , w command I get reply "killed"

Hi, After run ps , uptime , w command I get reply "killed" as normal dba and staff group user. As root every command works fine. I cheched all the user settings , right with other servers and I could not find any error and other settings. The oslevel is 5300-10-01-0921. Any idea to... (12 Replies)
Discussion started by: boki
12 Replies

7. Shell Programming and Scripting

can't run "date" command

A strange observation - $ ksh date ksh: date: cannot execute $ ksh "date" ksh: date: cannot execute $ ksh "date " Thu Sep 18 09:22:12 CDT 2008 why the date command doesn't run without a space ?? Please help (3 Replies)
Discussion started by: ajitkumar2
3 Replies

8. Shell Programming and Scripting

catalina.sh : need combination from "start" and "run"

heya, can someone help me with following problem. i am not sure how far you know the catalina.sh script from tomcat. when i start my tomcat with "catalina.sh run" then the startup-process-output will be printed out on the console, but the tomcat process is started in current shell/session, so... (1 Reply)
Discussion started by: Filly
1 Replies

9. UNIX for Dummies Questions & Answers

What is plan that comes when you use finger "usename" on solaris ?

Hai, In order to find out a user we can use finger "username" . The output of finger command has various details in the following manner : Login name: xyz In real life: xyz Directory: /home/xys Shell: /bin/ksh No unread mail No plan What is the plan... (1 Reply)
Discussion started by: ajphaj
1 Replies

10. UNIX for Dummies Questions & Answers

Run away "bootpgw" & "inetd"

Hello All. I'm get the following messages posted to the /var/adm/syslog file ever second and not sure on how to stop the process. May 14 15:50:52 a3360 bootpgw: version 2.3.5 May 14 15:50:52 a3360 inetd: /etc/bootpgw exit 0x1 As said about this gets logged every second only thing that... (4 Replies)
Discussion started by: cfaiman
4 Replies
Login or Register to Ask a Question