shell

Validate the number of arguments passed in bash from read

半腔热情 提交于 2021-02-05 06:06:18
问题 I have a question about validating user input regarding number of arguments passed by the user in a bash script. For example, if I use: if [[ $# -eq 2 ]] then... that will check if 2 arguments passed from the command line like this: ./somescript.sh arg1 arg2 but how to validate if user passed 2 arguments when asked? For example: echo "Type 2 names:" read... if [[ user passed more || less than 2 arguments]] echo "incorrect number of names" Now if I try to use $# -eq 2 it doesn't work. What's

Validate the number of arguments passed in bash from read

泪湿孤枕 提交于 2021-02-05 06:06:15
问题 I have a question about validating user input regarding number of arguments passed by the user in a bash script. For example, if I use: if [[ $# -eq 2 ]] then... that will check if 2 arguments passed from the command line like this: ./somescript.sh arg1 arg2 but how to validate if user passed 2 arguments when asked? For example: echo "Type 2 names:" read... if [[ user passed more || less than 2 arguments]] echo "incorrect number of names" Now if I try to use $# -eq 2 it doesn't work. What's

Save ssh -V to variable

余生长醉 提交于 2021-02-05 06:00:20
问题 I am trying to automate the testing of passwordless ssh from 72 remote servers back to a central server. I have central server passwordless ssh working to the 72 servers, but need it working from them back the the central server. The 72 servers have one of two ssh versions. OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 OR sshg3: SSH Tectia Client 6.1.8 on x86_64-unknown-linux-gnu Build: 136 Product: SSH Tectia Client License type: commercial The issue I am experience is trying to save

jq with multiple inputs from different sources

為{幸葍}努か 提交于 2021-02-05 05:59:05
问题 How can we mix different input sources when using jq ? For a specific usecase, I'd like to add some data from a file into a feed that was pipe in stdout. $ echo '[{"a": 1}]' > /tmp/a1 $ echo '[{"a": 2}]' > /tmp/a2 $ jq --slurp '.[0] + .[1]' /tmp/a1 /tmp/a2 [ { "a": 1 }, { "a": 2 } ] $ cat /tmp/a1 | jq --slurp '.[0] + .[1]' /tmp/a2 # Expecting the same result [ { "a": 2 } ] As you can see, the last command didn't interpret the piped data. Right now, I'm forced to save the output from the first

Is it possible to use functions defined in the shell from python?

我只是一个虾纸丫 提交于 2021-02-05 02:16:21
问题 Example: #!/bin/bash function my_test(){ echo this is a test $1 } my_test 1 python -c "from subprocess import check_output; print(check_output('my_test 2', shell=True))" output: this is a test 1 /bin/sh: my_test: command not found Traceback (most recent call last): File "<string>", line 1, in <module> File "/usr/lib/python3.5/subprocess.py", line 629, in check_output **kwargs).stdout File "/usr/lib/python3.5/subprocess.py", line 711, in run output=stdout, stderr=stderr) subprocess

replacing a placeholder in a html file with 1-n specific values defined in a config.json using shellscript [closed]

Deadly 提交于 2021-02-04 21:59:08
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 months ago . Improve this question The Problem: I need to replace a placeholder ( __PLACEHOLDER__ ) in a html file index.html with 1-n specific values defined in a config.json file. what I tried: replacing the placeholder inside index.html with new content: echo "${html//__PLACEHOLDER__/$replace}" > index.html

replacing a placeholder in a html file with 1-n specific values defined in a config.json using shellscript [closed]

浪子不回头ぞ 提交于 2021-02-04 21:58:23
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 months ago . Improve this question The Problem: I need to replace a placeholder ( __PLACEHOLDER__ ) in a html file index.html with 1-n specific values defined in a config.json file. what I tried: replacing the placeholder inside index.html with new content: echo "${html//__PLACEHOLDER__/$replace}" > index.html

Whats the difference between redirections “1>/dev/null 2>&1” and “ 2>&1 1>/dev/null”?

自作多情 提交于 2021-02-04 21:37:16
问题 Whats the difference between redirections 1>/dev/null 2>&1 and 2>&1 1>/dev/null It seems 1st one displays output to stdout but not the second one. Can somebody explain ! Thanks 回答1: Expanding on Lokendra26's answer a bit: /dev/null is a special file on your system, a device for discarding anything written to it. It's common to send output there if you don't want to see it. "File" in this case, and unix terminology in general, can be both a normal disk file, or a device like the null device or

Whats the difference between redirections “1>/dev/null 2>&1” and “ 2>&1 1>/dev/null”?

无人久伴 提交于 2021-02-04 21:37:04
问题 Whats the difference between redirections 1>/dev/null 2>&1 and 2>&1 1>/dev/null It seems 1st one displays output to stdout but not the second one. Can somebody explain ! Thanks 回答1: Expanding on Lokendra26's answer a bit: /dev/null is a special file on your system, a device for discarding anything written to it. It's common to send output there if you don't want to see it. "File" in this case, and unix terminology in general, can be both a normal disk file, or a device like the null device or

How to execute 4 shell scripts in parallel, I can't use GNU parallel?

风流意气都作罢 提交于 2021-02-04 21:35:42
问题 I have 4 shell scripts dog.sh, bird.sh, cow.sh and fox.sh. Each of these files execute 4 wgets in parallel using xargs to fork a separate process. Now I want these scripts themselves to be executed in parallel. For some portability reason unknown to me I can't use GNU parallel. IS there a way I can do this with xargs or with any other tool. Also can I also ask what could the portability reason be? I'm a total newbie to shell scripting. Sorry if my question seems cryptic. Thanks in advance