w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can a background bash script exit the running shell?
What seems like is happening is that after set -m is run in the script, the next command that is run is forced to be in the foreground process group, which takes the original shell out of the foreground process group. Once that process exits, the shell running the script is now in the foreground process group, but once that shell exits, the original shell doesn't put itself back into the foreground process group because it ran the script in the background. So you now have an interactive shell that is in a background process group. You can see some weird behavior here if you put a sleep at the end of your script so that it doesn't exit immediately. When you run the script in the background you get the terminal prompt back, but now your interactive shell isn't in the foreground process g

Categories : Bash

Evaluate output of a background linux command with C++ or Bash/Shell Script
If you run the command as shown, all output will go to stdout and display on the terminal. To capture the output you have a few options: Pipe the output to your monitor program, as in iw events | yourmonitorprogram which then reads stdin. iw should probably be modified to use unbuffered output. Write the output of iw to a file and then use the same technique as the tail -f command to poll the file periodically Have iw write to a named pipe or socket and have your monitor program read from that pipe or socket. This option requires modification to iw. The simplest option is the first one

Categories : C++

User Input for Variables in Bash Script
Yep, you'll want to do something like this: echo -n "Enter Fullname: " read fullname Another option would be to have them supply this information on the command line. Getopts is your best bet there. Using getopts in bash shell script to get long and short command line options

Categories : Bash

how to pass variable from python to shell script when shell script is getting called inside python script
You can: Step through the list, and then call mail.sh once for each file. This is the most reliable way. Convert the items in the list to a space delimited string, and then pass it in to mail.sh (assuming mail.sh is setup correctly, it should accept multiple arguments). What you cannot do is pass ['a.txt','b.txt','c.txt'] wholesale to the script.

Categories : Python

Stop avconv Bash script on user input and continue loop
UPDATED Try something like this (OP's code modified a little bit): #!/usr/bin/bash stop() { [ $CPID -ne 0 ] && kill -INT $CPID && wait $CPID && echo "Killed $CPID"; } trap "stop; exit" INT CPID=0 while :; do read -p "Enter your number here: " YOURNUMBER stop [ "$YOURNUMBER" == quit ] && break; TIMESTAMP=$(date +"%Y.%m.%d_%H.%M") avconv -f video4linux2 -r 3 -fs 52428800 -i /dev/video0 /home/$USER/orders/$YOURNUMBER/$TIMESTAMP-$YOURNUMBER.avi& CPID=$! echo -e "Video complete!" done This starts avconv in the background so the user can enter a new number. Maybe the stdout and stderr of avconv should be redirected to a log file or to /dev/null. Also it should be tested which signal stops avconv gracefully. -INT is equivalent to ctrl+c. It

Categories : Bash

sentence as user input - multiple times from terminal - bash script
This is because in your second call to read, you are using the -a argument which does: The words are assigned to sequential indices of the array variable aname, starting at 0. aname is unset before any new values are assigned. Other name arguments are ignored. That appears to be not what you want.

Categories : Bash

How can I execute a shell-script in a Bash sub-shell?
You can do the same but without eval. with-env-overrides() { ( source $HOME/.env-overrides "$@" ) } Example: $ cat ~/.env-overrides export A=1000 $ export | grep ^A= $ with-env-overrides export | ^grep A= declare -x A="1000" As you can see, in the second case, you've got an environment with the A variable.

Categories : Linux

bash shell script error works on command line, not in script
Variables are supposed to contain data, and bash treats them as data. This means that shell meta-characters like quotes are also treated as data. See this article for a complete discussion on the topic. The short answer is to use arrays instead: ASCIIDOC_OPTS=( --asciidoc-opts='-a lang=en -v -b docbook -d book' ) DBLATEX_OPTS=( --dblatex-opts='-V -T db2latex' ) cmd=(a2x -v -f pdf -L "${ASCIIDOC_OPTS[@]}" "${DBLATEX_OPTS[@]}" "$1".asciidoc) # Print command in pastable format: printf '%q ' "${cmd[@]}" printf ' ' # Execute it "${cmd[@]}" Make sure not to use eval: eval "$cmd" #noooo This will appear to work with your code as you posted it, but has caveats and security problems.

Categories : Bash

Issue with accepting user inputs in Jython script
input : raw_input = Python 3.x : Python 2.x If you want to run your program both in Python 3.x and Python 2.x (Jython 2.x), Put following lines at the beginning of your program. import sys if sys.hexversion < 0x3000000: input = raw_input

Categories : Python

Run a script in the same shell(bash)
You can still do something like: cd (specific benchmark directory) specinvoke & pid=$(pgrep milc_base.gcc43-64bit) If there are several invocation of the milc_base.gcc43-64bit binary, you can still use pid=$(pgrep -n milc_base.gcc43-64bit) Which according to the man page: -n Select only the newest (most recently started) of the matching processes

Categories : Linux

Accepting user input using a form and perform calculations & display it using javascript
input = document.getElementById('Enter').value; First of all, please, use "var" before initializing variables, unless "input" is global variable. Second, according to your code, input value will be "Enter". And right after that you try to multiply it by some numbers. Even JS cannot provide this kind of calculations. You need to use <input type="text" id="Enter" /> to allow users to write their own values. or you can use dialog window via prompt function. It will look like: function Air (){ while (isNaN(amount)) { var input = prompt("Please, enter needed amount. Notice, that it should be number"); var amount = parseFloat(input); } var TotalPurchasingAmountOrange = 0.87 * amount; var discountOrange = 0.13 * amount; var TotalPurchasingAmount

Categories : Javascript

How do I accept user input on command line for python script instead of prompt
Replace src = input('Enter Path to src: ') with: import sys src = sys.argv[1] Ref: http://docs.python.org/2/library/sys.html If your needs are more complex than you admit, you could use an argument-parsing library like optparse (deprecated since 2.7), argparse (new in 2.7 and 3.2) or getopt. Ref: Command Line Arguments In Python Here is an example of using argparse with required source and destination parameters: #! /usr/bin/python import argparse import shutil parser = argparse.ArgumentParser(description="Copy a file") parser.add_argument('src', metavar="SOURCE", help="Source filename") parser.add_argument('dst', metavar="DESTINATION", help="Destination filename") args = parser.parse_args() shutil.copyfile(args.src, args.dst) Run this program with -h to see the help message.

Categories : Python

Problems with shell script (sed) in bash
sed -i -b '/^.$/,$d' *.txt ##Deletes all blank lines and text following and including the first blank line You probably mean sed -i -b '/^$/,$d' *.txt Even further sed -i -b '/^[[:blank:]]*$/,$d' *.txt Which would include those lines with only spaces. On a test, this command (echo a; echo b; echo; echo b; echo; echo; echo c; echo d) | sed '/^$/,$d' Shows a b While this command (echo a; echo b; echo; echo b; echo; echo; echo c; echo d) | sed '/^.$/,$d' Shows nothing.

Categories : Bash

Incorrect result of bash shell script
Currently, you call exit whenever $response is not "y": [ "$response" = "y" ] || exit; To selectively execute some statements based on a condition, use the if-statement: if [ "$response" == "y" ] ; then cd /var/tmp/ ./gather.sh fi echo "continuing ..." This will continue with echoing "continuing ..." even when not entering "y".

Categories : Shell

run a "source" bash shell script in qmake
Short Answer: Using QMAKE_EXTRA_TARGETS and PRE_TARGET_DEPS, you can execute source /opt/intel/bin/compilersvars.sh intel64, but simply sourcing them will not solve your issue. Long Answer: The QMake file is converted into a Makefile. Make then executes the Makefile. The problem you will run into is that Make executes each command in its own shell. Thus, simply sourcing the script will only affect one command, the command that executes the script. There are a couple of possible ways to make things work: Execute the script before starting Qt-Creator. I've actually done this for some projects where I needed to have special environment variables setup. To make my life easier, I created a shell command to setup the environment and then launch Qt-Creator. Within Qt-Creator, modify the

Categories : Qt

Unix - Shell script to find a file from any directory and move it
find / -type f -name FILENAME | xargs -I foobar echo mv foobar /tmp (remove echo to make the command actually work .. i put it there just to save yourself from accidentally moving files just to try out the command) Note that -I foobar means that in mv foobar /tmp replace the foobar string with full path of the file found. for example, try: find / -type f -name FILENAME | xargs -I foobar foobar is a cool file

Categories : Shell

Script bash shell - Making an alternative trashcan
Quick and dirty solution: if [[ -f $TRASH$PARAM ]]; then mv "$PARAM1" "$TRASH$PARAM$RANDOM$RANDOM" # file exists else mv "$PARAM1" "$TRASH$PARAM" # ok, it is fine, file does not exist fi Also please note that you have to quote every variable in your script when it is passed as a parameter. if [ ${PARAM1:0:1} = "/" ]; then must be changed to if [ "${PARAM1:0:1}" = "/" ]; then or even better if [[ ${PARAM1:0:1} = "/" ]]; then mkdir -p $(dirname $TRASH$PARAM1) to mkdir -p "$(dirname "$TRASH$PARAM1")" And so on...

Categories : Bash

file region locking using bash shell script
As you use flock (1) (which is a C program, see http://util-linux.sourcearchive.com/documentation/2.17/flock_8c-source.html) to utilize flock (2), you would need a similar command that utilizes fcntl. If such a command doesn't exist yet, one would have to write it.

Categories : Linux

directory is not creating while running bash shell script in php
Check with which user's permissions it's run. You could echo the output of "whoami" (bash) command to know with wich user is used to run the script. If it's executed, for example, with the "www-data" user (ubuntu's [and maybe others] default httpd user), then it may not have the rights to create a directory in your user's home folder.

Categories : PHP

bash shell script not working as intended using cmp with output redirection
&> is bash syntax, you'll need to change the shebang line (first line) to #!/bin/bash (or the appropriate path to bash. Or if you're really using the Bourne Shell (/bin/sh), then you have to use old-style redirection, i.e. cmp ... >/dev/null 2>&1 Also, I think the &> was only introduced in bash 4, so if you're using bash, 3.X you'll still need the old-style redirections. IHTH

Categories : Bash

Shell/Bash script telnet testing a multicast address
Use netcat instead. -z - scan for listening daemons. -w 3 - timeout after 3 seconds #!/bin/bash host_list="www.google.com www.stackoverflow.com" port=80 for host in $host_list; do nc -z -w 3 $host $port > /dev/null 2>&1 if [[ $? = 0 ]]; then printf "%s%s%s " " [$host:" "UP" "]" else printf "%s%s%s " " [$host:" "DOWN" "]" fi done

Categories : Bash

Function failure - shell script converted from Bash to Korn
The function declaration syntax in ksh either uses the function keyword or the parentheses, but not both. Leave out either the parentheses or the function keyword and it should work.

Categories : Bash

Linux Bash Shell Script: How to "ls" a directory and check some output string?
To check for a directory: if [ ! -d '/var/aaaaaaa' ]; then echo 'no dir!' fi For file: if [ ! -f '/var/aaaaaaa' ]; then echo 'no file!' fi To check output: if ls '/var/aaaaaaa' 2>&1 | grep 'No such'; then echo 'no such'; fi To check when ls fails: if ! ls '/var/aaaaaaa' &> /dev/null; then echo 'failed' fi

Categories : Linux

background shell script terminating automatically
Ok, after hours and hours i finally found a solution, it might (must) be a bit dirty but it works ! As i said in a previous command, i used the trap command, here is my final script : #!/bin/sh #get the script directory SCRIPT=$(readlink -f "$0") script_path=$(dirname "$SCRIPT") #trap SIGHUP SIGINT SIGTERM and relaunch the script trap "pkill -9 inotifywait;($SCRIPT &);exit" 1 2 15 for f in `ls "$script_path"/data/` do php myscript.php "$script_path"/data/$f & done #watch the directory for file creation inotifywait -q -m --format %w%f -e create "$script_path"/data/ | while read -r line; do php myscript.php "$line" & done hope it will help shell beginner as me :) Edit : added "pkill -9 inotifywait" to make sure inotify process won't stack up,the parenthesis to mak

Categories : Bash

How to get result from background process linux shell script?
I finally found a working solution using anonymous pipes and bash: #!/bin/bash # this executes a separate shell and opens a new pipe, where the # reading endpoint is fd 3 in our shell and the writing endpoint # stdout of the other process. Note that you don't need the # background operator (&) as exec starts a completely independent process. exec 3< <(./a.sh 2&1) # ... do other stuff # write the contents of the pipe to a variable. If the other process # hasn't already terminated, cat will block. output=$(cat <&3)

Categories : Linux

running a command in shell script in parallel without using background
Jobs don't "just stop" because they're backgrounded, but you have to wait for them to finish to avoid exiting the script before they're done. Documentation

Categories : Shell

ImportError when calling python script via a shell script
I had a similar problem. It turned out that in my bash script I was running the Python script with a different version of Python than I was on the command line (my env was set up with virtualenv on Python 2.7 but I was calling the script with Python3 from the bash script). There is nothing in your snippets indicating this is the case but it is worth checking to make sure that the Python versions match up.

Categories : Python

dump files from bash script in different directory from where python script ran it
You should change directory within the same command: cmd = "/path/to/executable/executable" outputdir = "/path/to/output/" subprocess.call("cd {} && {}".format(outputdir, cmd), shell=True)

Categories : Python

In bash, how do I input a password for a background process command?
Usually this is done by not requiring a password at all. Instead, consider configuring SSH to use a public key. There are several resources online (such as this one from dreamhost) that can help you do that.

Categories : Linux

Control specific background subprocess in Bash script
I don't know if I get your question, but can't you operate on PID? You can get it/print it out from variable $!. bash child1 &; echo $!; And then just kill -ABRT that_pid. Or do you want to kill a process running a specific child script?

Categories : Bash

Trying to capture stdout of background process in bash script
Jenkins most likely looks at the process exit code to determine whether tests fails. This is what all Unix tools do. There are multiple ways of doing this. If your test files output something like "FAIL" instead of properly returning an exit code, you can do: #!/bin/bash ( ruby /root/selenium-tests/test/test1.rb & ruby /root/selenium-tests/test/test2.rb & ruby /root/selenium-tests/test/test3.rb & wait ) > log ! grep "FAIL" log exit $? # <- happens implicitly at the end of the script, and can be left out In this case, grep finding "FAIL" will cause the script to fail, and Jenkins to detect the failure. The more correct way, if your scripts return proper exit codes, is your method but without relying on job control (which by default is turned off in non-

Categories : Bash

Executing shell script that takes a input through ant
You may want to pass the $Orgname in the build.xml. <project name="Sample" default="automate"> <target name="automate"> <exec executable="/bin/bash"> <arg value="echo 'yak' | /root/test/Work/input.sh"/> </exec> </target> </project>

Categories : Unix

How to parse 2 input CSV files using shell script
Not sure what you want to do with the variables, but you can join the two files and then read it in a loop as shown below: join -t, -1 2 -2 1 <(sort -t, -k2 file1.csv) <(sort file2.csv) | while IFS=, read -r -a arr do var1=${arr[0]} var2=${arr[5]} echo "$var1 $var2" done You can also use awk to print out fields: join -t, -1 2 -2 1 <(sort -t, -k2 file1.csv) <(sort file2.csv) | awk -F, '{print $1,$5}'

Categories : Shell

run shell script in program and input parameters
I didn't remember exactly what the command was called, but it exists! Now, I have found it. The expect command will wait for a prompt like the one you have described, and then it will pretend to be a keyboard and it will enter data. A quick example: #!/usr/bin/expect set timeout 20 spawn "./parse" FOLDER="/x/y/z" LOGIN="Randy" PASSWORD="horse_stapler" PROJECTID="136729" expect "parse new " { send "$FOLDER" } expect "Login>" { send "$LOGIN" } expect "Pw>" { send "$PASSWORD" } expect "projectID>" { send "$PROJECTID" } interact ... more examples like this one here. BUT WAIT. There are 2 things that you need to know. Firstly, The reason that the top of the script is "#!/usr/bin/expect" is because this is not bash. Instead this is it's own special kind of script. Read mor

Categories : Shell

Conversion of shell script to python script
It looks like you're after a templating engine, but if you wanted a straight forward, no thrills, built into the standard library, here's an example using string.Template: from string import Template with open('a.html') as fin: template = Template(fin.read()) print template.substitute(name='Bob') # <head>Bob</head> I thoroughly recommend you read the docs especially regarding escaping identifier names and using safe_substitute and such...

Categories : Python

How to return a value from a shell script in a python script
use Subprocess.check_output instead of Subprocess.call. Subprocess.call returns return code of that script. Subprocess.check_output returns byte stream of script output. Subprocess.check_output on python 3.3 doc site

Categories : Python

Using a file input as stdin for a shell script does not work
What do you need the <&1 for? Remove it, and it works. while read CMD; do ./test.sh < input.txt BEGIN get_start END get_stop END get_uknown_command END

Categories : Linux

Why does this work in the Python IDLE shell but not when I run it as a Python script from the command prompt?
As @Sukrit Kalra says, don't use datetime.py as your file name. Python is getting confused with which datetime is which (and is importing itself!). Maybe; $ mv datetime.py my_datetime.py

Categories : Python

Shell Script that performs different functions based on input from file
Use read to input the lines. Do a case statement on their prefix: { while read DATA; do case "$DATA" in #*) ... switch function ...;; *) eval "$FUNCTION";; esac done } <inputfile Dependent on your problem you might want to experiment with setting $IFS before reading and read multiple variables in 1 go.

Categories : Linux

How can I break out of a shell script loop if the input is being handled by an inner "while read"?
#!/bin/bash mkfifo outputs for val in {1..10}; do echo "$RANDOM/20000" | bc | xargs sleep echo $val done > outputs & group=0 time=$(date +%s%N) while read line; do ctime=$(date +%s%N) [ $(( $ctime - $time )) -gt 1000000000 ] && let group++ echo "read time=$group read=$line" time=$ctime done < outputs

Categories : Bash



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.