shell

Evaluate expression inside bash for loop [duplicate]

别来无恙 提交于 2021-02-10 15:14:16
问题 This question already has answers here : How do I iterate over a range of numbers defined by variables in Bash? (20 answers) Closed 5 years ago . If I do this, I get the result as expected. for i in {125..129}; do echo $i; done 125 126 127 128 129 But when I do this? I get something weired. for i in {$((1+(25-1)*500))..$((25*500))}; do echo $i; done {12001..12500} I wish to pass a variable inside the loop variables like $((1+($j-1)*500)) 回答1: Bash's brace expansion has limitations. What you

How to check whether specific revision is present on some Mercurial remote?

与世无争的帅哥 提交于 2021-02-10 14:27:45
问题 In Git SCM I'm using the following command to check whether a specific revision is present on some remote: git fetch <remote> <revision> -q --dry-run If the exit code of the command is zero that means the revision is present on the remote. For Mercurial I tried the following command: hg pull <remote> -r <revision> -q It works but the problem is that it actually pulls the revision if present on the remote, but not locally. Is there an equivalent of the --dry-run option for Git, or some other

Why does this shell script work for one instance and not the other? [duplicate]

徘徊边缘 提交于 2021-02-10 13:17:14
问题 This question already has answers here : How to cat <<EOF >> a file containing code? (4 answers) Closed 2 years ago . I'm in need of some explanation of the following code, because they are the same concepts but not working the same. So, I'm trying to do the following: #!/bin/sh ssh -T username@host << EOF relative="$HOME/Documents" command=$(find \$relative -name GitHub) command2=$(echo \$relative) echo "HERE: \$command" echo "HERE: \$command2" EOF Here is the output I get: find: ‘$relative’

What does sleep 0 do in a shell script and what does it do if it used in the ansible SSH config to append after each command?

笑着哭i 提交于 2021-02-10 12:47:53
问题 What does sleep 0 do in a shell script? I read the man page for sleep and it says "delay for a specified amount of time" And the argument NUMBER specifies this time in SECONDS (by default). But I see ansible using sh -c 'echo ~ec2-user && sleep 0' to start with each task. Also, it uses this at the end of each remote command it is firing. I didn't find any special case mention of sleep 0 on the man page and based on the functionality of the sleep command it doesn't make any sense to have sleep

Bash regex with hyphen and dot

岁酱吖の 提交于 2021-02-10 12:23:06
问题 I'm trying to match hostname with regex. For some reason the following code fails. #!/bin/bash CONFIGURATION=m1si-ngxi-ddb01 #check configuration format TMP_CONFIGURATION=",${CONFIGURATION}" re=',[a-zA-Z0-9\-_\.]+' if ! [[ $TMP_CONFIGURATION =~ $re ]] then echo "configuration parttern mismatch." exit 1 fi Testing: [oracle@m1s-nyyy-db01 nir]$ vi a.sh [oracle@m1s-nyyy-db01 nir]$ 回答1: The pattern you have is failing due to "escaped" chars and the fact that - is not at the end/start of the

How to store command arguments which contain double quotes in an array?

不羁岁月 提交于 2021-02-10 06:45:50
问题 I have a Bash script which generates, stores and modifies values in an array. These values are later used as arguments for a command. For a MCVE I thought of an arbitrary command bash -c 'echo 0="$0" ; echo 1="$1"' which explains my problem. I will call my command with two arguments -option1=withoutspace and -option2="with space" . So it would look like this > bash -c 'echo 0="$0" ; echo 1="$1"' -option1=withoutspace -option2="with space" if the call to the command would be typed directly

Read JSON variable in Shell Script

六眼飞鱼酱① 提交于 2021-02-10 06:34:32
问题 How can I read a JSON variable in shell script ( to be noted "JSON variable " and not JSON File)? I have tried something like, temp={\"name\":\"Sipdy\",\"time\":\"17:09 1985\",\"place\":\"CA\"} jq '.time' $temp and also tried temp={"name":"Sipdy","time":"17:09 1985","place":"CA"} jq '.time' $temp but both the above commands expect a JSON file name in place of "$temp". 回答1: You need to provide the JSON text as jq's standard input: $ temp='{"name":"Sipdy","time":"17:09 1985","place":"CA"}' $

How do I use the output of an ls command in an if statement in a bash shell [duplicate]

穿精又带淫゛_ 提交于 2021-02-09 07:19:56
问题 This question already has answers here : Test whether a glob has any matches in bash (20 answers) Closed 6 months ago . I want to check if only one instance of a file exists before going on to process that file. I can check how many instances exist using this command: ls -l $INPUT_DIR/${INPUT_FILE_PREFIX}cons*.csv.gz | wc -l; However, when I go to use this within an if statement, I am warned that the ls: command not found . if [ls -l $INPUT_DIR/${INPUT_FILE_PREFIX}cons*.csv.gz | wc -l = '1']

How do I use the output of an ls command in an if statement in a bash shell [duplicate]

本小妞迷上赌 提交于 2021-02-09 07:19:01
问题 This question already has answers here : Test whether a glob has any matches in bash (20 answers) Closed 6 months ago . I want to check if only one instance of a file exists before going on to process that file. I can check how many instances exist using this command: ls -l $INPUT_DIR/${INPUT_FILE_PREFIX}cons*.csv.gz | wc -l; However, when I go to use this within an if statement, I am warned that the ls: command not found . if [ls -l $INPUT_DIR/${INPUT_FILE_PREFIX}cons*.csv.gz | wc -l = '1']

How do I use the output of an ls command in an if statement in a bash shell [duplicate]

北战南征 提交于 2021-02-09 07:09:43
问题 This question already has answers here : Test whether a glob has any matches in bash (20 answers) Closed 6 months ago . I want to check if only one instance of a file exists before going on to process that file. I can check how many instances exist using this command: ls -l $INPUT_DIR/${INPUT_FILE_PREFIX}cons*.csv.gz | wc -l; However, when I go to use this within an if statement, I am warned that the ls: command not found . if [ls -l $INPUT_DIR/${INPUT_FILE_PREFIX}cons*.csv.gz | wc -l = '1']