w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Can One Bash Script Launch Multiple Other Bash Scripts?
Run them in the background, just like you would in an interactive shell. command1 & command2 & command3 & wait # Wait for all background commands to finish The commands can be just about anything, not just other bash scripts.

Categories : Linux

Running a subprocess bash script from Python in the scripts current directory
Yep, Just before your loop programitically save the current working directory and change the current working directory to /some%s before the subprocess.call and then set it back to the original value when you are done. import os Orig = os.path.abspath('.') for i in range(1,100): os.chdir('/some%s' % i) subprocess.call(['./task.sh' % i, arg1]) os.chdir(Orig)

Categories : Python

Running a .py script from bash, how can I pass a python variable back to the terminal once the .py is done running?
You can write your variable and its value into an ini file from the python script. In Values.ini file my_var="value" After executing python script, just invoke Values.ini using . operator . Values.ini Now your shell variable $my_var will be having the assigned value. The advantage with this method is that you can assign any number of variables like this. var1="value1" var2="value2" var3="value3"

Categories : Python

Bash - my script renames a folder incorrectly
Your bash script has Windows newlines. Convert to Unix newlines with dos2unix or similar utility. If that doesn't help, open your script in a hex editor and remove the stray bytes at the end of that line.

Categories : Linux

bash script fails if `pwd -P` contains folder name with space in it
Use quotes to include spaces and characters in filenames. CURRENTDIR=`pwd -P` function make_sym_link { ln -s "$CURRENTDIR/$1" "$HOME/.$1"; } for FILE in janus irbrc gvimrc vimrc.after vimrc.before do make_sym_link $FILE done

Categories : Linux

Why does the child of an eval call from bash get orphaned once the parent bash script dies?
This is normal and expected. You sent a signal (presumably SIGTERM) to parent-script and it died, but no signal was sent to another-script. It keeps on running. This is different than what happens when the parent-script job is running interactively on a terminal and you type ^C (or ^Z). In that case, a SIGINT signal is automatically sent to the whole foreground process group. Since another-script is un the same process group as parent-script (by default), they both get the signal and they both die. If you want another-script to die automatically when its parent dies in any other context than when it's a job running in a terminal with job control, you have a few options. parent-script can trap the SIGTERM signal. In the signal handler, it kills its child, and then exits itself. This, o

Categories : Linux

BASH: how to define an array as environment variable before calling a bash script
Incredibility weird.... I have never seen that before. It looks like the array is not passed to the subshell. One way around this is to source the script instead of executing it: declare -a MYARR=( 1 2 ); . ./myscript.sh

Categories : Arrays

Pass a full bash script line to another bash function to execute
optout is a command like any other, and so must be preceded by any local modifications to the environment. The command that optout runs will inherit that environment. CC=${BUILD_TOOL_CC} optout ./configure ${ZLIB_CONFIGURE_OPT} --prefix=${CURR_DIR}/${INSTALL_DIR} By the way, this is just one of the problems you are likely to encounter with your optout function. You cannot run arbitrary command lines in that fashion, only a simple command followed by zero or more arguments (and I would expect there are some exceptions to even that restricted set, as well).

Categories : Linux

Folder Watcher bash script: also check for subfolders
You want to augment this line: prev=$(ls "$folder" | tr ' ' ' ' > /tmp/prev.txt) to something like: prev=$(ls "$folder"/* | tr ' ' ' ' > /tmp/prev.txt)

Categories : Bash

How to check if running as root in a bash script
If the script really requires root access then its file permissions should reflect that. Having a root script executable by non-root users would be a red flag. I encourage you not to control access with an if check. chown root:root script.sh chmod u=rwx,go=r script.sh

Categories : Bash

Running Bash Script Without ending it on Exit
Test script script call it remote.sh: at now <<! /path/to/test.h> /tmp/test.out ! copy this to the remote box: scp remote.sh thomas@remote: ssh remote 'chmod +x remote.sh' write the commands you want in a copy of test.sh locally, example: cd /tmp; cp *.foo /path/to/somewhere/else copy the script to the remote server scp test.sh thomas@remote: ssh remote 'chmod +x test.sh' ssh remote './remote.sh' Repeat steps 1, 2 & 3 each time you need to run your script without waiting for it.

Categories : Bash

'Globalize' Bash functions inside of a bash script
You can use this command in your startup.bash: source lib.bash the source command runs the file in the current shell environment, unlike using bash lib.bash (or . lib.bash) which creates a new, separate environment for that script (and only that script) and is why the function is not carried over. (source)

Categories : Linux

escaping bash 'test' keywork in a bash script
test is a Bash builtin command, not a keyword or reserved word. This means Bash will only interpret it if it is the first token in a command, like test blah In python manage.py test Bash will just pass it as a parameter to python (that is, it will execute python with two parameters, manage.py and test); you don't need to escape it. In general, Bash doesn't have reserved words in the sense of C-like languages. The only things you need to escape are whitespace (which Bash uses to separate params), and certain symbols including $(&|; which Bash uses for its variables and command separators.

Categories : Bash

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

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

Provide arguments (via bash script) to a process that is already running
You can use an anonymous pipe: # open a new file descriptor (3) and provide as stdin to myapp exec 3> >(run myapp) # do some stuff .... # write arguments to the pipe echo "arg1 arg2 -arg3 ..." >&3 The advantage over a named pipe is the fact that you don't need to worry about cleaning up and you won't need any write permissions.

Categories : Bash

How do you temporarily hide the output of a bash script that is running?
I can see two solutions: use screen (preferably), or use redirection and tail. For example: $ stdbuf -oL ./start.sh > /tmp/start.sh.log & $ tail -f /tmp/start.sh.log After doing this, you can see the log messages in your terminal. You can kill tail with impunity and start it again whenever you want to see the messages. When the log file gets too big, you can shorten it to zero with: $ echo -n > /tmp/start.sh.log Bash's redirect can seamlessly handle the file suddenly getting truncated. Not sure if this is true for all shells. You can interpose tee if that becomes a problem with your shell. I added the stdbuf -oL to “encourage” the shell-script to line-buffer stdout. Any commands in there that buffer their own output won't be affected, but it might help.

Categories : Linux

Run a bash script before running a Maven goal in IntelliJ Idea
Create an external tool to run bash with your script as a parameter. In the Maven Run configuration, Before launch section add this tool via the Run External Tool option.

Categories : Bash

Running Script to import database schema in to PostgreSQL via Bash
Maybe try to place it on the background with &: #!/bin/bash chown postgres:postgres /var/lib/pgsql/ddl.sql su - postgres cd ~ psql -Ubuild test < /var/lib/pgsql/ddl.sql & Make sure you run it in a script probably to prevent job control. If it pauses during process try to adding more configurations: set +o monitor psql -Ubuild test < /var/lib/pgsql/ddl.sql & disown And are you sure you still need to su to postgres when you already specify a different user with -Ubuild?

Categories : SQL

Status of perl scripts run from bash scripts
Perl scripts behave the same when run from the shell. Having test.pl: #!/usr/bin/env perl exit(0); # or just exit running: ./test.pl && echo "OK" || echo $? OK Having #!/usr/bin/env perl exit(123); running: ./test.pl && echo "OK" || echo $? 123 Having #!/usr/bin/env perl die; running: ./test.pl && echo "OK" || echo $? Died at ./test.pl line 2. 255 So: Exiting from perl script with 0 (or nothing) will be true in shell. Exiting from perl script with a (byte) value other than 0 will be false in shell and $? will give you the exit value. Exiting from perl script with die will be false in shell and $? will be 255 (in my case - I checked it).

Categories : Linux

Running a remote binary via a bash script is hanging if I give certain command line args
Put the ssh command in the background, not the remote command. for host in A B C do ssh user@$host " function test { cd /path/to/foo ./foo_exe --optA > run.out 2>&1 }; test" & done wait The reason is that ssh is waiting for the server to close the network connection before it exits. It doesn't go into the background just because the remote command is in the background. BTW, have you heard of pssh? http://www.theether.org/pssh/

Categories : Bash

Bash script that move files from one directory with lots of files to a month folder
something like this? DEBUG=echo cd ${directory_with_files} for file in * ; do dest=$(stat -c %y "$file" | head -c 7) mkdir -p $dest ${DEBUG} mv -v "$file" $dest/$(echo "$file" | sed -e 's/.* (.*)/1/') done DISCLAIMER: test this in a safe copy of your files. I won't be responsible for any lost of data ;-)

Categories : Bash

How to create a bash script that will lower case all files in the current folder, then search/replace in all files?
Something like this should work: #!/bin/bash for file in *.html do lowercase=`echo $file | tr '[A-Z]' '[a-z]'` mv "$file" "$lowercase" for f in *.html do sed -i "s/$file/$lowercase/g" "$f" done done Change *.html to *.<extension> if you're working with something other than html files.

Categories : Bash

bash scripts to compile and run C++
You are missing fi at the end, but you could also write a GNU Makefile like this: CXX=g++ CXXFLAGS=-Wall OBJS=main.o EXECUTABLE=main all: $(EXECUTABLE) $(EXECUTABLE): $(OBJS) $(CXX) $(CXXFLAGS) $(OBJS) -o $@ clean: rm -f $(EXECUTABLE) rm -f $(OBJS) main.o: main.cpp Note, this is a flexible example, and you could simplify this even further down the line. Here you can find a very good tutorial to read upon: http://mrbook.org/tutorials/make/ However, I would strongly suggest to learn cmake instead as writing Makefiles manually are painful. A CMakeLists.txt file could be something like this: add_executable(main main.cpp) then simply: mkdir -p build && cd build && cmake ../ && make VERBOSE=1 and then you would be done in a cross p

Categories : C++

CGI scripts for bash commands
Try this: #!/bin/bash echo "Status: 204 No Content" ...gpio stuff... HTTP responses must start with a status line; webservers will normally add status "200 Ok" if the CGI doesn't specify one. That status must be accompanied by response body, which will form the new web page. The status you want is 204, which indicates that the request was satisfied but that there is no response and the browser should stay on the same page. Normally, this would be a response to a POST request, not a GET request, but it should work anyway. Since a 204 response does not require a response body (in fact, it doesn't permit one), it should not be necessary to output a blank line following the status line, but you might need one if the script takes a long time to run.

Categories : HTML

Bash - secure scripts
This is not a bug in your script. Your invocation is equivalent to rm test ./test.sh " ; " There is nothing you can pass in as $1 that will cause test.sh to execute arbitrary commands. Apart from the ability to read any file, including infinite ones like /dev/zero and potentially blocking ones like /dev/stdin, the only potential issue is that you can pass in option instead, like -v. This is avoided with cat -- "$1" Bash scripts are generally data safe in themselves, but like SQL injection and system() calls, injection attacks can occur when e.g. sed, awk, tar and tons of other commands are not used correctly, and quoting errors can trigger logic bugs. These things are rarely related to shell syntax like `..` or $(..) and more related to seemingly innocent strings like "w foo", so

Categories : Apache

running one command, n time at the same time with different parameter like parallel for using bash script
May be you can use std::thread... #include <iostream> #include <thread> #include <string> void runcmd(std::string param) { // something like system(param.c_str()); } int main() { std::thread* threadarray[10]; for (int i=0; i<10; ++i) threadarray[i] = new std::thread(runcmd,"./a"); for (int i=0; i<10; ++i) { threadarray[i]->join; delete threadarray[i]; } return 0; } I think you can specify core id with sched_setaffinity. look: http://www.thinkingparallel.com/2006/08/18/more-information-on-pthread_setaffinity_np-and-sched_setaffinity/

Categories : C++

Running script on a remote server in bash working on 1 server, not on the other
It's just a guess but i would try this filename=hostnames > export.log while read -r -a line do hostname=${line//"} echo $hostname":" >> export.log ssh -t -t $hostname "bash -s" < script.sh >> export.log & done < "$filename" exit Just to make sure you don t get stuck on one server.

Categories : Bash

Add whatis hooks into bash scripts
whatis gets all the the information from whatis database, which is generated using the command makewhatis. There are two ways you can add an entry into the database. Create and install the man page for your program or script, then run makewhatis. Edit whatis database manually.(Not suggested) makewhatis picks up the first line from the NAME section of the man page of your command. The location of whatis database on my machine is /usr/share/man/whatis, here's how it looks like.. ABORT(7) - abort the current transaction ALTER_AGGREGATE(7) - change the definition of an aggregate function ALTER_COLLATION(7) - change the definition of a collation ALTER_CONVERSION(7) - change the definition of a conversion ... ping(8) - send ICMP ECHO_R

Categories : Linux

converting bash scripts to Python - how to handle heredocs?
Heredoc in Python Use multiline string (triple-quote string ''' or """). See Strings from tutorial. Run command import subprocess subprocess.Popen(['cat'], stdin=subprocess.PIPE).communicate(''' Hello multiline-string simliar to heredoc. ''')

Categories : Bash

Checking host availability by using ping in bash scripts
Ping returns different exit codes depending on the type of error. ping 256.256.256.256 ; echo $? # 68 ping -c 1 127.0.0.1 ; echo $? # 0 ping -c 1 192.168.1.5 ; echo $? # 2 0 means host reachable 2 means unreachable

Categories : Bash

how to get max number from a file with Linux bash shell scripts
Use sort: sort -t= -nr -k3 inputfile | head -1 For the given input, it'd return: log2c=3.0 rate=89.5039 If you want to read the values into variables, you can use the builtin read: $ IFS=$' =' read -a var <<< $(sort -t= -nr -k3 inputfile | head -1) $ echo ${var[1]} 3.0 $ echo ${var[3]} 89.5039

Categories : Bash

Can Linux Bash scripts respond to install options
To establish whether the scripts support any command line options, you can: Read the readme file of the software, if there is one. This should explain any options. Read the top of the install.sh script. Usually options are at least listed there. Read the code itself, which might be a lot of work. Once you have established that the scripts do not support options, the standard way of automating this would be with an expect script. See for example automating install.sh script using an expect script. Depending on how the install script works, you may be able to send it commands on standard input. For example, if you press Tab once, then Enter, then y and lastly Enter again, you can try the following: printf %s $' y ' | ./install.sh

Categories : Linux

bash script to write content into file. File content requires bash variables. How to do that?
You're actually deliberately turning off parameter subsitution by enclosing 'ENDFILECONTENT' in quotes. See this excerpt from example 19-7 of the advanced Bash scripting guide on Heredocs, slightly reformatted: # No parameter substitution when the "limit string" is quoted or escaped. # Either of the following at the head of the here document would have # the same effect. # # cat <<"Endofmessage" # cat <<Endofmessage Remove the single quotes around 'ENDFILECONTENT' and BASH will replace the variables as expected.

Categories : Bash

Handling Spaces In Substring Searches In Bash Shell Scripts
You can escape the space: $mainSubString = *blue cheese* or quote the non-wildcard portions, one example of which is $mainSubString = *'blue cheese'* Often, it is better to store the pattern in a variable, both to simplify the quoting and to make the [[...]] expression more concise. Note that you must not quote the parameter expansion, as glenn jackman points out in his comment. pattern="*blue cheese*" if [[ $mainString = *cat* || $mainSubstring = $pattern ]]; then

Categories : Bash

Good way to arrange variables and command calls in bash scripts
Don't know if this is any better than what you suggested, but: env GIT_EDITOR="some interesting command with 'quoting and' spaces" "$SOME_DIR/actual_command" argument

Categories : Bash

Is there a way to package bash shell scripts with AppleScriptObjC app on MacOSX with Xcode?
Answering my own question, after some hours of research and experimentation (with thanks to various posts on macscripter.net and autodidaktos.com): Add the script file, e.g., yourScriptFile.sh, to the project via the menu File - Add Files to (yourProject)... Include the script file in the app bundle so that it will be included with the app when distributed: In the Project Navigator or the Jump Bar select your project Select the "Build Phases" tab Expand the "Copy Bundle Resources" section (this applies to Xcode 4.6.3, maybe different with other versions) Add an item to the Bundle Resources by clicking the "+" button at the bottom of the list Select the script file to add to the bundle, e.g., yourScriptFile.sh In your AppleScriptObjC script, add the following subroutine on applica

Categories : Bash

Bash script to wait for gnome-terminal to finish before continuing script, only works for first instance of script
#!/bin/bash date bash -c "sleep 7" & bash -c "sleep 5" & wait date As you can see while running this script, both sleep commands will run in parallel, but main thread stalls, while they are running. Sat. Jule 27 01:11:49 2013 Sat. Jule 27 01:11:56 2013 Replace sleep 7 with expect launchneuron.exp and sleep 5 with expect launchmpj.exp and add your plot commands after calling "wait": echo "Simulation Complete" ...(your code to plot results)

Categories : Bash

Redirecting standard input in Bash implies that Bash executes a default action?
exec 6<&0 0</tmp/lines.txt; exec 0<&6 6<&- Turns out that bash still executes what remains in the command specified or script before it reads input for commands again that's why your file /tmp/lines.txt is not read since exec 0<&6 6<&- is run before it. Please refer back to the previous solution if you want to execute commands in /tmp/lines.txt before exec 0<&6- is executed. Also it might have not been obvious but you could use . or source to achieve same output: . /tmp/lines.txt source /tmp/lines.txt

Categories : Bash

execute user inputed Windows (or bash) commands from batch (or bash) file?
Do you need a full bash prompt? Or would something like this be enough? #!/bin/bash echo -n "Enter cmd: " read COMMAND echo ${COMMAND} | bash Also, in a script, you can just execute bash and get a full prompt in the current environment.

Categories : Bash



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