Sponsored Content
Top Forums Shell Programming and Scripting Questions related to if in awk context and if without awk context Post 303029697 by Don Cragun on Thursday 31st of January 2019 02:43:30 AM
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:
 

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:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy