sh

Splitting a string to tokens according to shell parameter rules without eval

只谈情不闲聊 提交于 2019-12-11 09:29:18
问题 I have a string like $ str="abc 'e f g' hij" and i wish to get whole e f g part of it. In other words, i wish to tokenize the string according to shell parameter rules. Currently, i am doing that as $ str="abc 'e f g' hij"; (eval "set -- $str"; echo $2) but this is totally unsafe if a single * gets outside of ' -ticks. Any better solutions? 回答1: You can use set -f to disable filename expansion altogether. $ str="* 'e f g' hij" $ ( set -f; eval "set -- $str"; echo $2 ) e f g This addresses

shell variables in qsub

若如初见. 提交于 2019-12-11 08:28:18
问题 I have this script which works fine when I call it with sh but it fails when I use qsub. Could someone please help me debug this? I can't seem to find an answer online #!/bin/bash #$ -S /bin/bash #$ -V #$ -cwd #$ -l h_vmem=6G #$ -N MHCIp if [ $# -lt 2 ] then echo need 2 arguments echo "USAGE : qsub run_MHCIprediction.sh <input_peptide_file> <MHCI_allele_file>" exit 0 fi input_file=$1 allele_file=$2 output_prefix=`echo ${input_file} | awk -F"." '{print $1}'` while read -u 10 allele strip

How to force /bin/bash interpreter for oneliners

耗尽温柔 提交于 2019-12-11 07:19:03
问题 I have a one liner bash command that is executed with python os.system() for FILENAME in mydir/PRE_*; do echo $FILENAME `printf ${FILENAME:0:12}; printf ${FILENAME:23}`; done; It loops through all files in a directory and renames them, to take chars 12-23 out of the filename. The command works fine when I execute it through bash with my server login, but when I executed it through python with the web user it throws errors. I found out that this is because the web user's default shell is /bin

Bash: total size of file in current directory matching regular expression

寵の児 提交于 2019-12-11 06:32:07
问题 ls | grep -P "$1" | awk '{ x += $5 } END { print "total bytes:" x }' This code is returning 0 using "[0-9]" as reg expr, in current folder there are 2 files named: "1" and "6", each one with size of 138bytes. Anyone could help me to find the problem? 回答1: You're just missing the -l option to ls . ls -l | grep -P "$1" | awk '{ x += $5 } END { print "total bytes:" x }' 回答2: du can help you with this; it has a -c flag for displaying a "grand total". It results in a final line containing the

How to run multiple entrypoint scripts one after another inside docker container?

非 Y 不嫁゛ 提交于 2019-12-11 06:09:17
问题 I am trying to match the host UID with container UID as below. Dockerfile RUN addgroup -g 1000 deploy \ && adduser -D -u 1000 -G deploy -s /bin/sh deploy USER deploy COPY entrypoint.sh / ENTRYPOINT ["/entrypoint.sh"] CMD ["php-fpm7","-F"] entrypoint.sh whoami # it outputs `deploy` # Change UID of 'deploy' as per host user UID HOST_CURRENT_USER_ID=$(stat -c "%u" /var/www/${PROJECT_NAME}) if [ ${HOST_CURRENT_USER_ID} -ne 0 ]; then gosu root usermod -u ${HOST_CURRENT_USER_ID} deploy gosu root

what is the purpose of #: (hashtag/pound colon) in bash

情到浓时终转凉″ 提交于 2019-12-11 05:53:57
问题 I was recently looking at the Homebrew source code. Since it's a popular tool, the code is probably relatively clean. I noticed they use a #: comment style (example: update.sh). I haven't seen that anywhere else (and it's hard to search for symbols, so I can't find any mentions of it). Is that an accepted convention? Does it have a special meaning? 回答1: It looks to be some sort of documentation fragment for a manpage: it is written in standard manpage style and documents the script that

why does this single quoted string get interpreted when it's inside of a command substitution

扶醉桌前 提交于 2019-12-11 05:43:38
问题 Compare these two lines of shell script: printf '%s' 's/./\\&/g' #1, s/./\\&/g printf '%s' `printf '%s' 's/./\\&/g'` #2, s/./\&/g My question is: why does the single-quoted double backslashes get interpreted as a single backslash for the second line of script? 回答1: Starting from printf '%s' `printf '%s' 's/./\\&/g'` The expression inside backticks returns s/./\\&/g as in the first expression, without single quotes, so you get printf '%s' s/./\\&/g The first backslash escapes the second one,

How to write a bash script which calls itself with python?

给你一囗甜甜゛ 提交于 2019-12-11 04:48:27
问题 Can someone explain how this bash script works? The part I don't understand is """:" , what does this syntax mean in bash? #!/bin/sh """:" echo called by bash exec python $0 ${1+"$@"} """ import sys print 'called by python, args:',sys.argv[1:] test running result: $ ./callself.sh xx called by bash called by python, args: ['xx'] $ ./callself.sh called by bash called by python, args: [] 回答1: That's clever! In Bash , the """:" will be expanded into only : , which is the empty command (it doesn't

make linux directory writable using bash command through java code

ぃ、小莉子 提交于 2019-12-11 03:38:24
问题 I have a directory /webroot in my linux server. Which permission set is 771, that means 1 for others. But I want to make it 773 through java code which like is : Process p1 = Runtime.getRuntime().exec("chmod 773 /webroot"); But it can not change the permission. I also capture the error stream which is like as : chmod: changing permissions of `/webroot/': Operation not permitted I saw a nearly related post yesterday, but i could not find the appropriate answer. At this time, what can I do now?

Using nested command substitution in jenkins pipeline sh step

人走茶凉 提交于 2019-12-11 03:16:16
问题 I'm trying to flatten a results directory prior to archiving using a symbolic link to the relevant inner folder that first traverses a bunch of variable path names determined at runtime. I can't quite seem to get the syntax right with this command substitution approach I picked up from this SO answer. Question: is there a rule for how to properly escape command substitution chains from a Jenkins pipeline step that I'm unaware of? Pipeline Script snippet post { always { sh """ echo 'Link to