I'm relatively new to unix command line and I do not really understand what this command does:
[[ ! -e "$text_file" ]] || { echo "Error: I cannot overwrite '$text_file'." >&2;}
Does anyone have an idea?
First of all, it checks 2 conditions with logical OR || operator.
-e FILE - True if the FILE exists and is a file,
so the condition is negating the result of this check.
and then if this is false then it goes and prints the message and send that to stderr (>&2). If the first condition is true then second condition won't be evaluated (of course this default logical OR way of working)
In Unix-world, stdout is generally used when everything is working correctly and stderr is generally used to print messages when something goes wrong.
By default, stdout and stderr both print to your screen. The main difference is that the > and | operators catch stdout by default, but not stderr.
You can also check other things like below:
-r FILE - True if the FILE exists and is readable.
-w FILE - True if the FILE exists and is writable.
-x FILE - True if the FILE exists and is executable.
-d FILE - True if the FILE exists and is a directory.
-e FILE - True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
-f FILE - True if the FILE exists and is a regular file (not a directory or device)
This checks a file for existence and the ! negates the expression
See a list of bash file testing
Related
I want to check if file exists are not in a Korn shell, but not able to get a proper documentation for that. I have the following code that checks if file exists and is of size zero. If the file size is more than zero it returns false.
if [[ ! -s ${abs_file_name} ]]
I need a list of possible options (like -s, -e, -x, etc like in the above example) with description to check if file exists in KORN shell, NOT BASH shell.
You can check if a node exists with
if [ -e "${abs_file_name}" ]
You can check if a node is a file.
if [ -f "${abs_file_name}" ]
This does something dumb if abs_file_name resolves to a symlink to a file.
Also: -d for directory, -r for you can read it, -x for executable.
My script has two functions running.
1. checking of file
(it has to check file in path /IIS/Data/arrival)
2. archiving of file
(it has to archive files in path /IIS/Data/archive)
how to pass this paths as parameter?
Not sure you are using shell script or not. In shell, we can pass path as parameter, shell treats it like string.
$ cat ./test.sh
#!/bin/sh
function check_file {
file=$1
ls $file
}
check_file $1
the output:
$ ./test.sh Downloads/bleachbit-1.10-1.1.fc20.noarch.rpm
Downloads/bleachbit-1.10-1.1.fc20.noarch.rpm
I'm a unix newbie here and I have a unix command I'm trying to run but I get a "GREP: not found" error. I looked at $PATH and didn't see anything resembling grep (not sure if thats what i'm looking for either though)...
The command is this:
testabcd=$(bteq << EOF 2>&1 |grep '^>' |sed -e "s/^>//"
.LOGON server/user, pass
DATABASE schema;
.set width 2000;
.set titledashes off;
SELECT '>'||COUNT(*) FROM schema1.table1;
.LOGOFF;
.QUIT;
.EXIT
EOF)
echo "The count is: " $testabcd
then I get these errors:
-ksh: SED: not found (No such file or directory)
>echo "The count is: " $testvarabcd
THE DATA IS:
>-ksh: GREP: not found
*** Error: The following error was encountered on the output file.
*** Error: Broke pipe
*** Warning: Canceling the rest of the output
if grep is not in PATH, do I need to install it? If not, can I set the path in the command and how do I search where the grep path is??
replace grep with /bin/grep and sed with /bin/sed.
ultimately you need to add /bin to your path.
do you like ksh? In my experience, csh, tcsh, or bash are
more commonly used. Using one of those may give you a better path.
Then you would not need to edit your path.
the file that contains you path is a hidden file (it has a . in front
of the file name) in your home directory. try ls .* in your home directory.
Even better, try
/bin/grep PATH .*
this will locate the file with the PATH variable.
I am trying to tail multiple files in a ksh. I have the following script:
test.sh
#!/bin/ksh
for file in "$#"
do
# show tails of each in background.
tail -f $file>out.txt
echo "\n"
done
It is only reading the first file argument I provide to the script. Not reading the other files as the argument to the script.
When I do this:
./test.sh /var/adm/messages /var/adm/logs
it is only reading the /var/adm/messages not the logs. Any ideas what I might be doing wrong
You should use double ">>" syntax to redirect the stream at the end of your output file.
A simple ">" redirection will write the stream at the beginning of the file and consequently it will remove the previous content.
So try :
#!/bin/ksh
for file in "$#"
do
# show tails of each in background.
tail -f $file >> out.txt & # Don't forget to add the last character
done
EDIT : If you want to use multi tail it's not installed by default. On Debian or Ubuntu you can use apt-get install multi tail.
I know 0 , 1, 2 are STDIN , STDOUT and STDERR file descriptors.
I am trying to understand redirection.
'>' means dump to a file
'>>' means append
But what does '>&' do ?
Also what is the step by step process for the following commands ?
command > file 2>&1
command > file 2<&1
Let's analyze it step by step:
>place means reopen the standard output so that it begins writing to place, which is a file name that will be open for writing. This is the typical redirection.
N>place does the same for an arbitrary file descriptor n. For example, 2>place redirects the standard error, file descriptor 2, to place. 1>place is the same as >place.
If place is written with the special syntax &N, it will be treated as an existing file descriptor number rather than a file name. So, >&2 and 1>&2 both mean reopen the standard output to write to standard error, and 2>&1 is the other way around.
The exact same goes for input, except place and the descriptors are opened for reading, and the file descriptor left of the < sign defaults to 0, which stands for standard input. 2<&1 means "reopen file descriptor 2 for reading so that future reads from it actually read from file descriptor 1". This doesn't make sense in a normal program since both file descriptors 1 and 2 are open for writing.
NUMBER1>&NUMBER2 means to assign the file descriptor NUMBER2 the file descriptor NUMBER1.
That means, to execute dup2 (NUMBER2, NUMBER1).
command > file 2>&1
Bash process the command line, it finds first the redirection >file, it changes stdout to be written to file, then continue to process and finds 2>&1, and changes stderr to be written to stdout (which is file in this moment) .
command > file 2<&1
this is the same, but 2<&1 redirects stderr to read from stdout. Because nobody reads from stderr, this second redirection normally has no effect.
However, bash treats this special case doing the same as for 2>&1, so executing dup2 (1, 2).
What does "2<&1" redirect do in Bourne shell?
2>&1 means redirect STDERR to the same place that STDOUT is going to. One example where it's useful is grep which normally works on STDOUT this makes it work on STDOUT and STDERR:
app 2>&1 | grep hello