sh

Minimal two column numeric input data for `sort` example, with distinct permutations

半城伤御伤魂 提交于 2019-12-08 12:17:36
问题 What's the least number of rows of two-column numeric input needed to produce four unique sort outputs for the following four options: 1. -sn -k1 2. -sn -k2 3. -sn -k1 -k2 4. -sn -k2 -k1 ? Here's a 6 row example, (with 4 unique outputs): 6 5 3 7 6 3 2 7 4 4 5 2 As a convenience, a function to count those four outputs given 2 columns of numbers, (requires the moreutils pee command), which prints the number of unique outputs: # Usage: foo c1_1 c2_1 c1_2 c2_2 ... foo() { echo "$@" | tr -s '[

Extracting a string from a file name

扶醉桌前 提交于 2019-12-08 12:13:37
问题 My script takes a file name in the form R#TYPE.TXT (# is a number and TYPE is two or three characters). I want my script to give me TYPE. What should I do to get it? Guess I need to use awk and sed. I'm using /bin/sh (which is a requirement) 回答1: you can use awk $ echo R1CcC.TXT | awk '{sub(/.*[0-9]/,"");sub(".TXT","")}{print}' CcC or $ echo R1CcC.TXT | awk '{gsub(/.*[0-9]|\.TXT$/,"");print}' CcC and if sed is really what you want $ echo R9XXX.TXT | sed 's/R[0-9]\(.*\)\.TXT/\1/' XXX 回答2: I

How to expand bash variable in a string and preserve newline

為{幸葍}努か 提交于 2019-12-08 10:00:20
问题 This one works for single line string: var2="2018" str="${var1:-hello} world! Happy $var2 new year $var2" newstr=() for cnt in "$str" ;do echo "$cnt" [ "${cnt:0:1}" == '$' ] && cnt=${cnt:1} && cnt=${!cnt} newstr+=($cnt) done newstr="${newstr[*]}" How to preserve the newline? 回答1: Even if I fail to fully understand what your goal is, correctly quoting will preserve the new lines. Change this two lines: [ "${cnt:0:1}" == '$' ] && cnt="${cnt:1}" && cnt="${!cnt}" newstr+=("$cnt") 回答2: The purpose

How can I exec an interactive shell from Jython?

馋奶兔 提交于 2019-12-08 07:54:39
问题 I need some example Jython, or Java, code to exec an interactive OS shell (/bin/sh). Our development environment is Java on Linux. What I would like to do is provide a means for developers to write Jython scripts, and then drop into a shell, which allows them to inspect the system. The reason I need to exec this from Jython or Java--as opposed to just wrapping all this in a shell script--is that we use Java load some libraries which I need to keep loaded while the interactive shell is running

How to get exact list of collections form mongo database in shell script

馋奶兔 提交于 2019-12-08 07:35:55
问题 I would like to get the collection names list from mongo database. So, I use the following command in shell script : collections= mongo $dbName --eval "db.getCollectionNames()" The output result of this command is "MongoDB shell version: 2.2.0 connecting to: cm_v2 col1,col2,col3,col4" I would like to get only the collections name such as : col,col2,col3,col4. So, how should I delete the output like version from result. 回答1: use --quiet flag collections=mongo $dbName --quiet --eval "db

Different pipeline behavior between sh and ksh

混江龙づ霸主 提交于 2019-12-08 05:42:21
问题 I have isolated the problem to the below code snippet: Notice below that null string gets assigned to LATEST_FILE_NAME='' when the script is run using ksh ; but the script assigns the value to variable $LATEST_FILE_NAME correctly when run using sh . This in turn affects the value of $FILE_LIST_COUNT . But as the script is in KornShell (ksh), I am not sure what might be causing the issue. When I comment out the tee command in the below line, the ksh script works fine and correctly assigns the

'sed' replace last patern and delete others pattern

随声附和 提交于 2019-12-08 05:42:17
问题 I want to replace only the last string "delay" by "ens_delay" in my file and delete the others one before the last one: Input file: alpha_notify_teta='' alpha_notify_check='YES' text='CRDS' delay='' delay='' delay='' textfileooooop='' alpha_enable='YES' alpha_hostnames='' alpha_orange='YES' alpha_orange_interval='300' alpha_notification_level='ALL' expression='YES' delay='9' textfileooooop='' alpha_enable='YES' alpha_hostnames='' Output file: (expected value) alpha_notify_teta='' alpha_notify

How can I properly escape bash variables when being quoted in various ways

删除回忆录丶 提交于 2019-12-08 05:13:47
问题 I have the following code: set -o xtrace openDirectory () { lxterminal --command="zsh -c 'cd "$1"; zsh -i' " } # Handle argument. if [ "$@" ] then openDirectory ~/Projects/Clients/"@1" cd $1 fi This fails if there is a space in the argument passed. lxterminal '--command=zsh -c '\''cd /home/chris/Projects/Clients/Test' - 'Space; zsh -i'\'' ' cd Test - Space cd: too many arguments How can I properly escape this? Is this even feasible to be done with something like bash? 回答1: Assuming the

How to do Git pull only on a branch in shell script?

放肆的年华 提交于 2019-12-08 05:11:25
问题 I have Bourne shell script that will build the HEAD of a specified remote branch or a specified remote tag. The branch/tag is specified as input argument to the script. In the script I perform a git fetch to update all local info with the remote info. Then I perform a git checkout branch/tag. I then like to perform a conditional git pull (if I understand it correctly). In case it concerns a branch I like to perform a git pull to merge from the remote branch with the same name. However, in

How to format The mail Output of Bash Shell Script

ⅰ亾dé卋堺 提交于 2019-12-08 04:58:43
问题 I have a Bash shell script which send an email in every day at certain time. the code is as follows: first_dir=/test1 second_dir=/test2 email=me@me.com allfiles=$(find /test1 /test2 -maxdepth 1 | sort) IFS=$'\n' while true do sleep 24h [ "$allfiles" != "" ] && find $allfiles -maxdepth 1 -printf '%Tc\t%s\t%p\n' | mail -s "List Of All Files" "$email" files="$allfiles" done This script is giving output in Single column. but I want the Output in two columns. 1st column with files of first_dir=