ksh

Copy stderr and stdout to a file as well as the screen in ksh

六眼飞鱼酱① 提交于 2019-12-03 17:25:40
I'm looking for a solution (similar to the bash code below) to copy both stdout and stderr to a file in addition to the screen within ksh on Solaris. The following code works great in the bash shell: #!/usr/bin/bash # Clear the logfile >logfile.txt # Redirect all script output to a logfile as well as their normal locations exec > >(tee -a logfile.txt) exec 2> >(tee -a logfile.txt >&2) date ls -l /non-existent/path For some reason this is throwing a syntax error on Solaris. I assume it's because I can't do process substitution, and I've seen some posts suggesting the use of mkfifo , but I've

Best practices for holding passwords in shell / Perl scripts?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 17:02:50
问题 I've recently had to dust off my Perl and shell script skills to help out some colleagues. The colleagues in question have been tasked with providing some reports from an internal application with a large Oracle database backend, and they simply don't have the skills to do this. While some might question whether I have those skills either (grin), apparently enough people think I do to mean I can't weasel out of it. So to my question - in order to extract the reports from the database, my

ksh: how to probe stdin?

左心房为你撑大大i 提交于 2019-12-03 16:57:28
I want my ksh script to have different behaviors depending on whether there is something incoming through stdin or not: (1) cat file.txt | ./script.ksh (then do "cat <&0 >./tmp.dat" and process tmp.dat) vs. (2) ./script.ksh (then process $1 which must be a readable regular file) Checking for stdin to see if it is a terminal[ -t 0 ] is not helpful, because my script is called from an other script. Doing "cat <&0 >./tmp.dat" to check tmp.dat's size hangs up waiting for an EOF from stdin if stdin is "empty" (2nd case). How to just check if stdin is "empty" or not?! vladr EDIT: You are running on

How to expand shell variables in a text file?

血红的双手。 提交于 2019-12-03 09:50:16
问题 Consider a ASCII text file (lets say it contains code of a non-shell scripting language): Text_File.msh: spool on to '$LOG_FILE_PATH/logfile.log'; login 'username' 'password'; .... Now if this were a shell script I could run it as $ sh Text_File.msh and the shell would automatically expand the variables. What I want to do is have shell expand these variables and then create a new file as Text_File_expanded.msh as follows: Text_File_expanded.msh: spool on to '/expanded/path/of/the/log/file/..

Create a pipe that writes to multiple files (tee)

房东的猫 提交于 2019-12-03 07:49:01
I would like to create a pipe in a ksh script (using exec) that pipe's to a tee, and sends the output to a pipe. Current: #Redirect EVERYTHING exec 3>&1 #Save STDOUT as 3 exec 4>&2 #Save STDERR as 4 exec 1>${Log} #Redirect STDOUT to a log exec 2>&1 #Redirect STDERR to STDOUT What'd I'd like to do (but I don't have the syntax correct): #Redirect EVERYTHING exec 3>&1 #Save STDOUT as 3 exec 4>&2 #Save STDERR as 4 exec 1>tee -a ${Log} >&3 #Redirect STDOUT to a log exec 2>&1 #Redirect STDERR to STDOUT How can I create this pipe? Here's a solution I use. It works under ksh on my Mac. It's nicely

How can I find a file/directory that could be anywhere on linux command line?

眉间皱痕 提交于 2019-12-03 07:22:24
问题 Ideally, I would be able to use a program like find [file or directory name] to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder. I've also tried locate and which, but none find the file, even though I know its on the computer somewhere. 回答1: "Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use find

Is it necessary to specify traps other than EXIT?

牧云@^-^@ 提交于 2019-12-03 06:36:17
问题 I see a lot of shell scripts that do: trap cmd 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM In every shell I have access to at the moment, all the traps other than 0 are redundant, and cmd will be executed upon receipt of a signal if the trap is simply specified: trap cmd 0 Is the latter specification sufficient, or do some shells require the other signals to be specified? 回答1: I think trap 0 is executed just prior to script termination in all cases, so is useful for cleanup functionality

Get exit code for command in bash/ksh

夙愿已清 提交于 2019-12-03 06:12:00
问题 I want to write code like this: command="some command" safeRunCommand $command safeRunCommand() { cmnd=$1 $($cmnd) if [ $? != 0 ]; then printf "Error when executing command: '$command'" exit $ERROR_CODE fi } But this code does not working the way I want. Where I made mistake? 回答1: Below is the fixed code: #!/bin/ksh safeRunCommand() { typeset cmnd="$*" typeset ret_code echo cmnd=$cmnd eval $cmnd ret_code=$? if [ $ret_code != 0 ]; then printf "Error : [%d] when executing command: '$cmnd'" $ret

Best practices for holding passwords in shell / Perl scripts?

亡梦爱人 提交于 2019-12-03 05:58:59
I've recently had to dust off my Perl and shell script skills to help out some colleagues. The colleagues in question have been tasked with providing some reports from an internal application with a large Oracle database backend, and they simply don't have the skills to do this. While some might question whether I have those skills either (grin), apparently enough people think I do to mean I can't weasel out of it. So to my question - in order to extract the reports from the database, my script is obviously having to connect and run queries. I haven't thus far managed to come up with a good

Shortest command to calculate the sum of a column of output on Unix?

删除回忆录丶 提交于 2019-12-03 01:28:38
问题 I'm sure there is a quick and easy way to calculate the sum of a column of values on Unix systems (using something like awk or xargs perhaps), but writing a shell script to parse the rows line by line is the only thing that comes to mind at the moment. For example, what's the simplest way to modify the command below to compute and display the total for the SEGSZ column (70300)? ipcs -mb | head -6 IPC status from /dev/kmem as of Mon Nov 17 08:58:17 2008 T ID KEY MODE OWNER GROUP SEGSZ Shared