Questions related to if in awk context and if without awk context
I wrote this code, questions follow
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.
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.
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.
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:
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.
-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.
returns this:
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.
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:
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!) 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.
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:
and then using:
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:
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:
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
produces the output:
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 awkif 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:
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)
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)
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)
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)
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)
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,
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)
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)
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)