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:
When I enter any other value at the prompt, I get the output:
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:
as if it had been written as:
(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:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)