ksh

How to read just a single character in shell script

核能气质少年 提交于 2019-11-26 21:08:29
问题 I want similar option like getche() in C. How can I read just a single character input from command line? Using read command can we do it? 回答1: In ksh you can basically do: stty raw REPLY=$(dd bs=1 count=1 2> /dev/null) stty -raw 回答2: In bash, read can do it: read -n1 ans 回答3: read -n1 works for bash The stty raw mode prevents ctrl-c from working and can get you stuck in an input loop with no way out. Also the man page says stty -raw is not guaranteed to return your terminal to the same state

How to mkdir only if a dir does not already exist?

旧城冷巷雨未停 提交于 2019-11-26 18:02:30
I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exists" error that mkdir throws when it tries to create an existing directory. Any thoughts on how best to do this? Try mkdir -p : mkdir -p foo Note that this will also create any intermediate directories that don't exist; for instance, mkdir -p foo/bar/baz will create directories foo , foo/bar ,

How to add text at the end of each line in unix

懵懂的女人 提交于 2019-11-26 15:58:51
问题 I am doing certain text processing operations and finally able to get a file something like this india sudan japan france now I want to add a comment in the above file like in the final file it should be something like india | COUNTRY sudan | COUNTRY japan | COUNTRY france | COUNTRY like a same comment across the whole file. How do I do this? 回答1: There are many ways: sed : replace $ (end of line) with the given text. $ sed 's/$/ | COUNTRY/' file india | COUNTRY sudan | COUNTRY japan |

What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

你离开我真会死。 提交于 2019-11-26 12:06:32
问题 I just want to understand following line of code in shell. It is used to get the current working directory. I am aware that $(variable) name return the value inside the variable name, but what is $(command) supposed to return? Does it return the value after executing the command? In that case, we can use ` to execute the command. CWD=\"$(cd \"$(dirname $0)\"; pwd)\" Same output can be taken from the following line of code also in different version of shell DIR=\"$( cd \"$( dirname \"${BASH

how to merge two files consistently line by line

风流意气都作罢 提交于 2019-11-26 11:57:29
问题 I have two files ( file1.txt & file2.txt ) , files are only examples . How to merge the two files , in order to create the file - merge_files.txt as example 3 I writing now ksh script , so merge can be done with ksh,awk,sed,perl one liner ...etc Background - why I need to merge the files : my target is to rename the OLD file (exist in first field) to NEW file (exist in second field) , example1 more file1.txt /etc/port1-192.9.200.1-255.555.255.0 /etc/port2-192.9.200.1-255.555.255.0 /etc/port3

How to mkdir only if a dir does not already exist?

久未见 提交于 2019-11-26 08:53:52
问题 I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the \"File exists\" error that mkdir throws when it tries to create an existing directory. Any thoughts on how best to do this? 回答1: Try mkdir -p: mkdir -p foo Note that this will also create any

How to detect if a script is being sourced

余生颓废 提交于 2019-11-26 01:37:06
问题 I have a script where I do not want it to call exit if it\'s being sourced. I thought of checking if $0 == bash but this has problems if the script is sourced from another script, or if the user sources it from a different shell like ksh . Is there a reliable way of detecting if a script is being sourced? 回答1: This seems to be portable between Bash and Korn: [[ $_ != $0 ]] && echo "Script is being sourced" || echo "Script is a subshell" A line similar to this or an assignment like pathname="$

How to detect if a script is being sourced

江枫思渺然 提交于 2019-11-25 23:37:59
I have a script where I do not want it to call exit if it's being sourced. I thought of checking if $0 == bash but this has problems if the script is sourced from another script, or if the user sources it from a different shell like ksh . Is there a reliable way of detecting if a script is being sourced? Dennis Williamson This seems to be portable between Bash and Korn: [[ $_ != $0 ]] && echo "Script is being sourced" || echo "Script is a subshell" A line similar to this or an assignment like pathname="$_" (with a later test and action) must be on the first line of the script or on the line

What is the difference between $(command) and `command` in shell programming?

陌路散爱 提交于 2019-11-25 22:05:01
问题 To store the output of a command as a variable in sh/ksh/bash, you can do either var=$(command) or var=`command` What\'s the difference if any between the two methods? 回答1: The backticks/gravemarks have been deprecated in favor of $() for command substitution because $() can easily nest within itself as in $(echo foo$(echo bar)) . There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc. See BashFAQ/082 for several reasons to always prefer the $(..