sh

What is the most aesthetic way to to escape a single quote within a single-quoted string in (ba)sh?

谁说我不能喝 提交于 2019-12-20 07:37:45
问题 In a (ba)sh script of mine, I have, for example: MYVAR='Alice says: "Hello, Bob." But Bob isn't listening.' This is a syntax error, since the ' in isn't ends the single-quoted string. I know I can fix this using MYVAR='Alice says: "Hello, Bob." But Bob isn'"'"'t listening.' But that is sooo ugly... what can I do instead? sh doesn't support MYVAR='Alice says: "Hello, Bob." But Bob isn\'t listening.' Which would have been tolerable, and switching to a double-quotes string is not an option for

Shell script — weird behaviour after concatenating string with variables

女生的网名这么多〃 提交于 2019-12-20 05:55:59
问题 I am reading a .properties file from my shell script. I wanted to read some value for some key and after that want to append it in between some string but the output is weird. #!/bin/bash # Script used to read Property File FILE_NAME="Test.properties" prop_value=$(cat ${FILE_NAME} | grep Address) echo "ABC${prop_value}DEF" my Test.properties is like this Name=Pravin Age=25 Address=Mumbai asd=asd After executing this script I am expecting ABCAddress=MumbaiDEF but I am getting output like

Reading complete line in 'for' loop with spaces, tabs with multiple input files

拈花ヽ惹草 提交于 2019-12-20 04:56:17
问题 I have seen articles of 'for' loop. It splits on the occurance of whitespace like space, tab, or newline. To get ride of that issue i have following extra line of command: IFS=$'\n' But when i try to solve the above scenario on following details (i have two files: 'input1.txt' and 'input.txt' on my current directory): BASH command: bash script.sh 'input*' Below is 'for' loop block in script.sh for line in $(cat $1) ... ... done; I got following error on execution: cat: input1.txt input.txt*:

sort on pipe-delimited fields not behaving as expected

怎甘沉沦 提交于 2019-12-20 03:50:28
问题 Consider this tiny text file: ab a If we run it through sort(1), we get a ab because of course a comes before ab . But now consider this file: ab|c a|c If we run it through sort -t'|' , we again expect a to sort before ab , but it does not! (Try it under your version of Unix and see.) What I think is happening here is that the -t option to sort is not really delimiting fields -- it may be changing the way (say) the start of field 2 would be found, but it's not changing the way field 1 ends .

initializing arrays in sh

旧时模样 提交于 2019-12-20 02:58:09
问题 I want to initialize an array in sh. In bash that would be: list=(`seq 1 4`) In sh I try to do it like this: for i in `seq 1 4`; do list[$((i-1))]="$i" done I get an error though for each iteration saying: list[0]=1: not found What am I doing wrong and how to fix that? 回答1: POSIX sh doesn't support arrays. You need a more advanced shell for that, e.g. bash , zsh , or ksh . 回答2: If you really want to use arrays, you can fudge them by writing your own array function. I'm not going to encourage

How to run .sh files in Windows?

北城以北 提交于 2019-12-20 02:57:17
问题 I download some repositories of DojoToolkit in GitHub that have .sh files, I know .sh extension are typically shell scripts written in Unix, but exist a way to run this in Windows? 回答1: cygwin is probably the easiest way. http://www.cygwin.com/ Alternatively if you're already using git-bash for github etc you can use it to run .sh scripts too. 回答2: Porting unix stuff to windows is sometime possible (mostly thanks to cygwin, as pointed out by other answers) but except for simple stuff, you'll

POSIX SH build loop variable with elements containing spaces

£可爱£侵袭症+ 提交于 2019-12-20 01:07:46
问题 Here's the code I need: #!/bin/sh x1="a1 a2" x2="b1 b2" list=SOMETHING for x in "$list" do echo $x done And the output I want: a1 a2 b1 b2 The question is: what should SOMETHING be? I want $list to behave just as $@ does. Notes: I can't use $IFS and I can't eval the entire loop. 回答1: This is probably as close as you can get: #!/bin/sh x1="a1 a2" x2="b1 b2" set -- "$x1" "$x2" for x in "$@" do # echo $x echo "[${x}]" # proves that the lines are being printed separately done Output: [a1 a2] [b1

Find out if file has been modified within the last 2 minutes

社会主义新天地 提交于 2019-12-19 12:27:21
问题 In a bash script I want to check if a file has been changed within the last 2 minutes. I already found out that I can access the date of the last modification with stat file.ext -c %y . How can I check if this date is older than two minutes? 回答1: I think this would be helpful, find . -mmin -2 -type f -print also, find / -fstype local -mmin -2 回答2: Complete script to do what you're after: #!/bin/sh # Input file FILE=/tmp/test.txt # How many seconds before file is deemed "older" OLDTIME=120 #

Want value of $bhai in javascript

僤鯓⒐⒋嵵緔 提交于 2019-12-19 10:52:08
问题 This question was migrated from Super User because it can be answered on Stack Overflow. Migrated 2 years ago . #!/bin/sh echo "Content-type: text/html" echo "" echo '<html>' echo '<head>' echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' echo '</head>' echo '<body><center><br><h3 align='center'>STATUS</h3></br></center>' list=$(ls -l /tmp | grep "^d" | awk -F" " '{print $9}') list1=$(echo $list | wc -w) i=1 while [ $i -le $list1 ] do bhai=$(echo $list | cut -d' ' -f

Run script in background?

*爱你&永不变心* 提交于 2019-12-19 10:28:13
问题 Simple question: Is there a way to run a script in the background with out terminal running? More detail and background: I had an app that read an apps .log file and puled information from it, then provide information and statistics from the information in the log. An update to the app changed the way the .log file was written and delete information and duplicates the log in a manner that i have been unable to predict. the app that was designed to interface with the log was not coded to check