Sponsored Content
Top Forums Shell Programming and Scripting Questions related to if in awk context and if without awk context Post 303029701 by Seth on Thursday 31st of January 2019 04:11:31 AM
Old 01-31-2019
Quote:
Originally Posted by Don Cragun
Please always tell us what operating system and shell you're using when you start a new thread in the Shell Programming and Scripting forum. Many of the commands you're using in your bash script will behave differently on different operating systems. It is much easier to make suggestions that will work in your environment if we know what environment you're using.

Code:
 
OS: Linux Lite 4.0

 uname -a = Linux seth-desktop 4.15.0-23-generic #25-Ubuntu SMP Wed May 23 18:02:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
 
 
  bash -version = GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
 Copyright (C) 2016 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later (link removed do to not having 5 posts yet)

 This is free software; you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.

-Thank you for replying, Don. My code works when $input is a file in the pwd, but as stated I get a fatal error when $input is not actually a file in the working directory, and commands under else do not execute. Finding where I went wrong is what I'm looking for in the 1st question. When $input is actually a file in pwd, the if condition returns true, and the fields specified ($1 and $2) are printed along with the tab separating them. So code works but only if $input is a file in the directory. Else doesn't work if $input isn't in the directory.


The way I have coded the script allows for the if-expression to take the condition as expressed and returns true if the file is present, but if I am not using awk, for instance outside the script, as in: (at prompt)


Note* AfileinDir is a file present in the pwd, not calling for any of the info in it, just trying to understand why awk/if returns true as in my script, and why if with the bracket syntax outside the script doesn't.



Code:
input=“AfileinDir” 

          if [ $input == echo$(ls) ];  # or if [ $input == $(ls) ]
             then echo $input " was found via if condition"
             else echo $input " was not found via if condition"
          fi

returns this:
Code:
          bash: [: too many arguments
          AfileinDir was not found via if condition

you see how this is different from when I used awk in my script with the alternate syntax. The awk/if statement yielded a true response and no errors (if the file was in the directory) and it printed out just the fields requested with the tab separating them. Using if[] outside the script and without awk as above returns a negative response and “too many arguments” error. Using the double brackets [[]] around the condition yields a negative response but no error. I am trying to understand the why of all this to better understand the commands and syntax rather than look for an alternative method. I will, however, look over your code to see what I can learn from it, thank you.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Context Switching

I know that this is a relative question but can someone give me an idea of what would be considered a high number of context switches? I am running vmstat and show a cs value of between 5000 and 6000 on a 4 processor system. How can I deduce if this number is high or not? Also, the timeslice... (2 Replies)
Discussion started by: keelba
2 Replies

2. Shell Programming and Scripting

keep context in awk

here is a data file. ------------------------------------- KSH, CSH, BASH, PERL, PHP, SED, AWK KSH, CSH, BASH, PERL, PHP, BASH, PERL, PHP, SED, AWK CSH, BASH, PERL, PHP, SED, KSH, CSH, BASH, PERL, PHP, SED, AWK ------------------------------------- My desired output is... (2 Replies)
Discussion started by: VTAWKVT
2 Replies

3. UNIX for Advanced & Expert Users

context lost problem

there are several same servers(process) on more than one server(machine) providing the same service. we store session/context within the server(process), if the same client login, he will be directed to the very server service for him last time. But, if a server(machine or process) down, the... (1 Reply)
Discussion started by: zhongyj
1 Replies

4. Shell Programming and Scripting

grep help after context

Hi, There's a file with below contents which I have to read based on the input parameter provided by the user. FILE_ID=1 FILE_FTP_ID=ftp.server1.com FILE_FTP_USER=user1 FILE_FTP_PASS=pass1 FILE_ID=2 FILE_FTP_ID=ftp.server2.com FILE_FTP_USER=user2 FILE_FTP_PASS=pass2 FILE_ID=3... (6 Replies)
Discussion started by: dips_ag
6 Replies

5. UNIX for Dummies Questions & Answers

Split based on Context

Hello, I have a file that I want to be able to split at specific positions. For example in the file below I want to be able to split at every occurence of 2 at the start of a line, into multiple files. 2 abc PQRST abcRSTG 2 cde FGKL abcLKGRG ABCLKgrg 2 lmn OPT lmopqrst uvwxyz ... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

6. UNIX for Dummies Questions & Answers

Context-switching question

Hi all, I've got this question that i need to solve: "Type `vmstat -s; vmstat -n 1 5; vmstat -n 1 5; vmstat -s` to your Ruby interpreter. Then terminate your Ruby session. Run the Unix com- mand vmstat -s; vmstat -n 1 5; vmstat -s in the same terminal window you had been using for Ruby. Did... (1 Reply)
Discussion started by: snowboarder
1 Replies

7. Slackware

Context dependent symlinks

Ive got multiple PCs, sharing an NFS mounted home dir. For certain apps I would like to keep the config files host specific. Easy solution is to create symlinks to local folders for configs. Ideally I would still want the .config files to reside in the user home folder. Is it possible to... (2 Replies)
Discussion started by: agentrnge
2 Replies

8. Linux

involuntary context switching

In a kernel based on 2.6.27: In the schedule() routine they have a local variable switch_count: /* * schedule() is the main scheduler function. */ asmlinkage void __sched schedule(void) { struct task_struct *prev, *next; unsigned long *switch_count; struct rq... (2 Replies)
Discussion started by: chriskot
2 Replies

9. UNIX for Advanced & Expert Users

Interupt Context Switching

If suppose a middle level interrupt is being serviced and a high priority interrupts comes in then in that case what all process will take place. The interrupt context switch will happen. But where will the interrupt context be saved? Is there something called as part process data area? (4 Replies)
Discussion started by: rupeshkp728
4 Replies

10. Shell Programming and Scripting

Context for use of [.symbol.] awk notation

Hi Just wondering ... do you have an example of context that would demonstrates how usefull the awk notation can efficiently be used ? Thx :rolleyes: (6 Replies)
Discussion started by: ctsgnb
6 Replies
All times are GMT -4. The time now is 04:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy