ksh

How to custom display prompt in KornShell to show hostname and current directory?

纵饮孤独 提交于 2019-12-02 23:46:00
I am using KornShell (ksh) on Solaris and currently my PS1 env var is: PS1="${HOSTNAME}:\${PWD} \$ " And the prompt displays: hostname:/full/path/to/current/directory $ However, I would like it to display: hostname:directory $ In other words, how can I display just the hostname and the name of the current directory, i.e. tmp or ~ or public_html etc etc? From reading the ksh man page you want PS1="${HOSTNAME}:\${PWD##*/} \$ " Tested on default ksh on SunOS 5.8 Okay, a little old and a little late, but this is what I use in Kornshell: PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}"

Is it necessary to specify traps other than EXIT?

最后都变了- 提交于 2019-12-02 20:12:06
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? Brandon Horsley I think trap 0 is executed just prior to script termination in all cases, so is useful for cleanup functionality (like removing temporary files, etc). The other signals can have specialized error handling but

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

对着背影说爱祢 提交于 2019-12-02 19:56:09
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. "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 -name "filename" If the file in question is not in the current working directory, you can search your entire

Get exit code for command in bash/ksh

Deadly 提交于 2019-12-02 18:42:23
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? havexz 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_code exit $ret_code fi } command="ls -l | grep p" safeRunCommand "$command" Now if you look into

Single command to create a file and set its permission

半城伤御伤魂 提交于 2019-12-02 18:02:20
I am using the following 2 commands to create a 0B file and set its extn to 644 touch filename.ext chmod 777 filename.txt My question is that whether there is any single command in unix (korn shell) that will do the two things together that is to say create a 0B fil with desired permission? proski install -m 777 /dev/null filename.txt For bash , simply use chmod with file redirection and history expansion: chmod 777 filename.txt>>!#:2 For ksh and zsh you have to drop the history expansion (as shown above, there may be other ways) and use: chmod 644 filename>>filename For scripts of any shell

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

故事扮演 提交于 2019-12-02 16:42:53
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 Memory: m 0 0x411c322e --rw-rw-rw- root root 348 m 1 0x4e0c0002 --rw-rw-rw- root root 61760 m 2

Bash or KornShell (ksh)? [closed]

China☆狼群 提交于 2019-12-02 14:09:27
I am not new to *nix, however lately I have been spending a lot of time at the prompt. My question is what are the advantages of using KornShell (ksh) or Bash Shell? Where are the pitfalls of using one over the other? Looking to understand from the perspective of a user, rather than purely scripting. Bash. The various UNIX and Linux implementations have various different source level implementations of ksh, some of which are real ksh, some of which are pdksh implementations and some of which are just symlinks to some other shell that has a "ksh" personality. This can lead to weird differences

Running a Block of Shell Script remotely using SSH

馋奶兔 提交于 2019-12-02 13:08:40
I am try to execute a block of commands on a different server using a shell script Can anyone please help me on this while [ $RecordCount -gt 0 ] do expXXXXX=`sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d "|" -f1` exprXXXXXn_id=`sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f2` run_dt=`sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f3` #START OF THE BLOCK - IN SERVER 2 if [ -d "/sas/ADH/exXd_$expXXXXX" ]; then if [ -d "/sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id" ]; then mv -f /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/latest/* \ /sas/ADH/exXd_$expXXXXX

Scoping problems in different shell languages?

回眸只為那壹抹淺笑 提交于 2019-12-02 11:19:14
问题 It appears that pdksh and mksh has the scoping implementation I expected. For example: readonly x='global' f() { local x readonly x='f' echo $x } g() { local x readonly x='g' echo $x } echo $x f g echo $x pdksh and mksh produce my expected result: global f g global And Bash fails: line 5: local: x: readonly variable Dash and Ksh93 failed my expect, too. (I've changed local to typeset in Ksh93's test.) This seems confusing. UPDATE: I've edited the question. The question before is not stated in

How to sort Integer Array in ksh | Unix Shell Scripting

懵懂的女人 提交于 2019-12-02 09:49:48
问题 How to sort integer array in KornShell. Found this link, KornShell Sort Array of Integers but it seems to be not working and throwing error. Code: NUM_ARR[1]=-1 NUM_ARR[2]=-2 NUM_ARR[3]=-3 NUM_ARR[4]=-4 NUM_ARR[5]=-5 NUM_ARR[6]=-6 NUM_ARR[7]=-7 for file in /home/fimsctl/datafiles/outbound/timelog/timelog_file_*.csv ; do SORTED_NUM_ARR=`($(printf "%s\n" ${NUM_ARR[@]} | sort -n))` echo ${SORTED_NUM_ARR[*]} done Output: testb.ksh[118]: -7: not found 回答1: You can use sort with process