Questions related to if in awk context and if without awk context


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Questions related to if in awk context and if without awk context
# 1  
Old 01-30-2019
Questions related to if in awk context and if without awk context

I wrote this code, questions follow


Code:
#! /bin/bash -f 

 # Purpose - to show how if syntax is used within an awk
  clear;
  ls -l;

  echo "This will print out the first two columns of the inputted file in this directory";
  echo "Enter filename found in this directory";
  read input;
  awk '{if($input == echo$(ls)) 
         {
          print $1 "\t" $2;
         }
         else
          {
          echo "file " $input " not in this Directory";
          exit
          }
       }' $input


Question 1 - how would I write this better so that my commands are executed under the else statement instead of getting a fatal error when $input is not a file in the Directory?


Question 2 - how would I represent this if statement outside this script and not within an awk command? When using if [ $input == echo$(ls) ] or if [[ $input == echo$(ls) ]]
where $input is a file name in the Directory I get a "too many arguments" and false return or just a false return respectively.



Thank you in advance.
# 2  
Old 01-30-2019
Is this a homework assignment? Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If this is homework, please explain where the code you presented came from. There are so many syntax errors that it is hard to know where to start explaining the problems.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-31-2019
Quote:
Originally Posted by Don Cragun
Is this a homework assignment? Homework and coursework questions can only be posted in this forum under special homework rules
(url's from your quote deleted as I have less than 5 posts)


This is not a homework assignment. I am learning scripting on my own.


Quote:
Originally Posted by Don Cragun
Please review the rules, which you agreed to when you registered, if you have not already done so.
(url deleted from quote, for same reason above)


I have reviewed the rules. This is not a homework assignment. I am asking for input from people with more knowledge than me to improve my learning.


Quote:
Originally Posted by Don Cragun
If this is homework, please explain where the code you presented came from. There are so many syntax errors that it is hard to know where to start explaining the problems.

Again, this is not a homework assignment, and I came up with the code myself. The code works in it's simple purpose, but I am looking to improve it as stated in my first question, including any syntax errors. I am also seeking to understand the difference in the if function between awk and non-awk, as stated in my second question.


Quote:
Originally Posted by Don Cragun
If you did post homework in the main forums, please review the guidelines for posting homework and repost.
(urls deleted)


This is not a homework assignment, and as I am not enrolled in any type of course or program, any posts I make in the foreseeable future will not be.



Thank you.
# 4  
Old 01-31-2019
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.

When I run your script in a directory containing two files (one named problem and one named tester) and enter either of those names when prompted, I get the output:
Code:
total 24
-rw-r--r--  1 dwc  staff  1077 Jan 30 22:34 problem
-rwxr-xr-x  1 dwc  staff   457 Jan 30 23:34 tester
This will print out the first two columns of the inputted file in this directory
Enter filename found in this directory
problem
awk: illegal field $(), name "input"
 input record number 1, file problem
 source line number 1

When I enter any other value at the prompt, I get the output:
Code:
total 24
-rw-r--r--  1 dwc  staff  1077 Jan 30 22:34 problem
-rwxr-xr-x  1 dwc  staff   457 Jan 30 23:34 tester
This will print out the first two columns of the inputted file in this directory
Enter filename found in this directory
unknown
awk: can't open file unknown
 source line number 10
awk: can't open file unknown
 source line number 8

where unknown is whatever string I entered when prompted.

This is what I got when using bash and awk on macOS Mojave version 10.14.2. Different versions of awk might give you different diagnostic messages or might always treat the expression in your awk if statement:
Code:
if($input == echo$(ls))

as if it had been written as:
Code:
if($0 == $0)

(because you have defined the shell variable input by your bash read statement, but you have not defined a variable named input in your awk script. And using an undefined variable in an awk script causes it to be treated as either an empty string (when a string is expected) or as 0 (when a number is expected). Since none of the awk variables in your awk if statement are defined, I would expect that input will be treated as 0 (because a field number is expected after a dollar sign in awk), echo will be evaluated as an empty string because you are concatenating two strings (whatever echo expands to and whatever $(ls) expands to) and since ls expanded to an empty string on my version of awk I got a syntax error. If the code you showed us does print the 1st two columns from each line of a file in the currrent directory when you enter the name of a file in the current directory, apparently ls expanded to a 0 in your version of awk. In that case comparing the entire contents of any input line ($0) to itself ($(0)) will always yield true and print the 1st two fields of each line of the file using <tab> as a separator in the output.

No matter what version of awk you use, if you invoke it with one pathname operand and that pathname does not name an existing file, you will get an error message similar to the one I specified above or the one you alluded to in question 1 in post #1 in this thread.

Since you didn't really give us a definition of what you are trying to do with your script, I can only make wild guesses. If what I am guessing is correct, there is no reason to use awk at all. It can all be done in bash (or any shell that conforms to the POSIX standards) with something like:
Code:
#!/bin/bash

# This script provides a long listing of the current directory and asks the user
# to end the name of one of the files in the directory.  If the name of a file
# in the current directory is given, the first two fields on each line will be
# printed separated by a <tab> character.  Otherwise a diagnostic message will
# be printed.

# Clear the screen.
clear

# Provide a long listing of the files in the current directory.
ls -l

# Prompt for and read the name of a file to be processed.
echo 'This will print out the first two columns of the inputted file in this directory'
printf 'Enter the filename of a regular file found in this directory: '
read input

if [ -f "$input" ]
then	# The name of an existing regular file was given.  Print the first two fields.
	while read -r field1 field2 rest
	do	printf '%s\t%s\n' "$field1" "$field2"
	done < "$input"
else	# The name given does not name an existing file.
	echo "file \"$input\" not in this Directory"
	exit 1
fi

Note that I only tested for the presence of a regular file. Trying to read a directory or most other file types using the read utility isn't something you're ready to try handling yet.

The shell if statement in the above code could be replaced by a printf piped into an awk script, but it would be considerably more complicated than the above shell script.

Is this approximately what you were trying to do?

Last edited by Don Cragun; 01-31-2019 at 03:45 AM.. Reason: Fix typo: s/that/than/
This User Gave Thanks to Don Cragun For This Post:
# 5  
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.
# 6  
Old 01-31-2019
I really appreciate your sole quest to master *nix and associates challenges, and I'd like to support you with it. Let's start with a few comments:
Don't intermix the various topics / fields, which you do in your first post, mixing shell and awk syntax. Concentrate on one only first, then extent your work to others. Don't work with non-*nix tools (windows(?) editors) so not to introduce off-system errors (e.g. locale double quotes) leading to syntax problems.
Let's start with some comments on your last post:
Code:
input=“AfileinDir”

Syntax error! The double quotes are NOT the required ASCII " (oct 042, dec 34, hex 22). What editor do you use? Change to a genuine *nix one, or at least switch yours to *nix mode. While we're at it, make sure your script lines are terminated correctly (\n in *nix, NO \r!)
Code:
if [ $input == echo$(ls) ];  # or if [ $input == $(ls) ]

bash: [: too many arguments: echo$(ls) evaluates to like "echofile1 file2 etc", i.e. the string "echo" immediately followed by the list of files in your directory. Will rarely fit anything in your $input variable. Would ls $input do what you want (check if file exists in current working dir)?


Don Cragun already pointed out many aspects of the failure of your approach. I just want to emphasize that without a basic understanding of the operation of both shell and awk and the sometimes subtle differences between the two, and, no less important, the ways and methods they interact, it will become difficult to compose a working solution.

Last edited by RudiC; 01-31-2019 at 06:06 AM..
# 7  
Old 01-31-2019
I explained the reasons why the awk code you supplied won't work. You can choose to ignore my comments and continue to wonder why your code doesn't work. Trying to use shell variables in awk and assume that shell variable expansions will work in awk will never work the way you have used them because awk is not bash. After defining a variable in bash to be the name of a file with:
Code:
read input

and then using:
Code:
awk -v input="$input" 'BEGIN { print "input contains: \"" input "\""}'

shows you how you can turn a shell variable into an awk variable that can be used inside an awk script. Note that in shell:
Code:
echo $VAR

prints the value of the string stored in the variable named VAR (assuming that the string assigned to VAR does not contain any <backslash> characters, does not start with a <hyphen> possibly following leading <space> and/or <tab> characters). (If those constraints are broken, the output produced by echo varies from shell to shell and operating system to operating system.)

You get roughly the same output inside awk with the awk statement:
Code:
print VAR

if and only if VAR is also an awk variable containing the same string as the shell variable VAR.

Using $1 in a shell script refers to the contents of the 1st command line argument passed to your shell script. Using $1 in an awk script refers to the contents of the 1st field on the current record you are processing from the current file you are reading with awk.

If the shell variable input contains the string 5 and the awk variable input input contains the string 5 then in shell code $input expands to the string 5, but in awk code $input expands to the string that is the contents of field number 5 in the current input line in the current input file.

In a directory where I have hundreds of files and the first file in the directory (sorted alphanumerically) is named 1999_08-09.sum, the shell command
Code:
echo$(ls)

produces the output:
Code:
bash: echo1999_08-09.sum: command not found

because the shell didn't find a utility named echo1999_08-09.sum after concatenating the output produced by the command substitution $(ls) with the string echo.

The same code in awk (when there is no variable in awk named echo and no variable in awk named ls with your version of awk expands to $(0) which awk treats as the contents of the current input line from the current input file. This obviously has absolutely nothing to do with the name of a file stored in a shell variable.

You can't mix random shell statements and random awk statements and assume that that mix will magically be interpreted the way you want it to be interpreted.

The shell command language and the awk command language are not the same no matter how much you want that to be true. If you want to use shell variables in an awk script you have to create an awk variable that contains the contents of that shell variable. If you want to run shell commands inside an awk script, you have to learn the awk commands that can be used to do that (and the awk you have shown us doesn't make any attempt to do so).

BSD, Linux, and UNIX systems provide you with hundreds of tools you can use to do all sorts of wondrous things. But you have to first learn that those tools only fit together in certain ways. And, there are certain tools that are extremely good at doing one thing and extremely poor at doing other things. And, using an awk if statement to determine if a string assigned to a shell variable names an existing file is an extreme case of using the wrong tool to try to do the job.

I wish you luck in your adventures, but I sincerely hope you'll take a closer look at the (entirely) bash script I gave you that seems to do what I think you were trying to do. Trying to use awk to determine whether or not there is a file of a certain name in the current working directory is enormously more difficult than learning to use the test utility that is available in all shells based on Bourne shell syntax (e.g., bash, dash, ksh, sh, and zsh).

You could also use [[ expression ]] instead of test expression or [ expression ], in some versions of bash and some versions of ksh, but I strongly suggest that you learn the basics before trying to use shell specific features that work well in some cases but not in others.

Last edited by Don Cragun; 01-31-2019 at 06:52 AM..
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question