w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Makefile: run bash script via 'make all' command BEFORE global variables initialization
This doesn't sound like a good design, but here's one way to do it: all: bash-script $(MAKE) other-things other-things: some-prereq $(FOO) other-stuff $(BAR)

Categories : C++

Command works in command line but gives 'no such file or directory' in bash script
Remove the quotes around line 8. Like this: ./optAlg.exe "$inputFile" 500 2>&1 > "output/$outputFile" By placing quotes around the whole line you tell bash to execute a command which is called exactly ./optAlg.exe $inputFile 500 2>&1 > output/$outputFile and of course there is no such command. In reality you want to run ./optAlg.exe with parameters. Do not forget to place quotes around the variables because otherwise filenames that have whitespace characters are going to be passed as several arguments. Please read about arguments. And you can read about common pitfalls as well.

Categories : Bash

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

How to make this command open a new terminal & execute the command within it ( Exec=bash -c "tail -n 10 -f /var/log/httpd/error_log" )
To open a terminal Exec=nameOfYourTerminalApp (eg:gnome-terminal, lxterminal, xterm) Then you can run your commands in the opened terminal. Otherwise, you can create a script with some command and run it. In that case you have to put tail -n 10 -f /var/log/httpd/error_log in the script and make it executable. Then call it with a shortcut file.

Categories : Linux

Run bash script on `cd` command
One feature of Unix shells is that they let you create shell functions, which are much like functions in other languages; they are essentially named groups of commands. For example, you can write a function named mycd that first runs cd, and then runs other commands: function mycd () { cd "$@" if ... ; then workon environment fi } (The "$@" expands to the arguments that you passed to mycd; so mycd /path/to/dir will call cd /path/to/dir.) As a special case, a shell function actually supersedes a like-named builtin command; so if you name your function cd, it will be run instead of the cd builtin whenever you run cd. In that case, in order for the function to call the builtin cd to perform the actual directory-change (instead of calling itself, causing infinite recurs

Categories : Python

Sed command in bash script
The simplest approach is to use the tr ("translate characters") utility, telling it to translate newlines to commas: tr ' ' , < input_file > output_file

Categories : Bash

PostgreSQL Command fom bash script
sudo -u postgres psql TestDB -c "select count(*) from config where var_name='url' The Database was missing, so you executed the command into postgres db where you probably don't have that table ;-)

Categories : Bash

Bash Script to function as wc command
You need to build the options in the case statement and then execute wc: # Set WC_OPTS to empty string WC_OPTS=(); while getopts lwc choice do case $choice in l) WC_OPTS+='-l';; w) WC_OPTS+='-w';; c) WC_OPTS+='-c';; ?) echo wrong option. esac done # Call wc with the options shift $((OPTIND-1)) wc "${WC_OPTS[@]}" "$@"

Categories : Bash

BASH script, !=: command not found
That's not the way to compare it. if $OUTP != average: Should be if [[ $OUTP != average: ]] Some line seemed odd as well OUTP= uptime | awk '{print $10}' | cut -f1 -d, I think should be OUTP=$(uptime | awk '{print $10}' | cut -f1 -d,) And then $OUTP >> $Oad.lst if you want to send the value of the variable to the file, it should be then echo "$OUTP" >> "$Oad.lst" Overall I would suggest a format like this: #!/bin/bash Oad=$(date +%Y_%m_%d.%H.%M.%S) for i in {1..120}; do OUTP=$(uptime | awk '{print $10}' | cut -f1 -d,) echo "$OUTP" if [[ $OUTP != average: ]]; then echo "$OUTP" >> "$Oad.lst" sleep 60 fi done awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1<min) {min=$1}; total+=$1; count+=1} END {print to

Categories : Bash

How can i run a sudo command in Bash script?
You shouldn't pass username and password. This is not secure and it is not going to work if the password is changed. You can use this: gksudo kinit # This is going to open a dialog asking for the password. #sudo kinit # or this if you want to type your password in the terminal vi hello.txt Or you can run your script under root. But note that vi is going to be ran as root as well, which means that it will probably create files that belong to root, that might be not what you want.

Categories : Bash

Execute command in variable bash script
Since I'm running a shell script, it uses a sub-shell so it cannot access the parent shell's environment. Problem solved running the shell script in this way: . ./script.sh

Categories : Shell

Postgresql command execution in bash script
Locate your psql binary with which psql you will get output like /usr/bin/psql Now you can use the complete path in your script or extend your $PATH Just a short note! Don't save your password within the script. If you need the (psql) password for certain users you should save it in .pgpass host:db:user:password and give it the proper permission (i.e. 0600)

Categories : Linux

Stop bash script without command line
I think it is a little bit overcomplicated. Actually you do not need to start bash as it is started when You start ./executeServer.sh. Add executable rights to ./executeServer.sh and start it as it is. And do not need to start it in the background, as it already starts Server in the background in the script.

Categories : Bash

Edit File in bash script by sed command
Looks to me like those sed commands arguments are not on the same line and line shift is not escaped. Try: sed -e "s/$SEEDVALUE/$RANDOMVAR/" -e "s/$EXPIREMENT/$QUAN/" -e "s/$DISTANCE/$DIST/" -e "s/$OUTPUTFILE/$outputText/" < $TEMPLATEFILE > $outputFile Also, to get more debugging information, it's often useful to use the following as interpreter: #!/bin/bash -x

Categories : Bash

bash script with grep and cut : command not found
In this line: result=`$callResult >> grep 'RESPONSE CODE' | cut -d':' -f 2` You should be piping output to grep, not redirecting. Change it to this: result=`$callResult | grep 'RESPONSE CODE' | cut -d':' -f 2` Also, the syntax is a bit off, and you're better off avoiding backticks when possible. This is even better: result="$(echo ${callResult} | grep 'RESPONSE CODE' | cut -d':' -f 2)"

Categories : Bash

using date command in bash script: next occurrence of 3pm
You could always check the hour first with something like: if [[ $(date +1%H) -ge 115 ]] ; then date -d "tomorrow 15:00:00" +%s else date -d "15:00:00" +%s fi The 1 on the front of the date format and the hour 15 is to ensure two possibilities returned from date +%H don't get treated as invalid octal numbers (08 and 09). That's actually a bit of a kludge so you can use numeric comparisons in conjunction with date itself: if [[ $(date +%s) -ge $(date -d "15:00" +%s) ]] ; then date -d "tomorrow 15:00" +%s else date -d "15:00" +%s fi This won't generate numbers beginning with 0 so the octal problem is irrelevant. It will also work with arbitrary times like 13:22:17. The following transcript shows this is action: pax> date Wednesday 21 August 15:46:16 WST 2013 pax&

Categories : Bash

How to replace command name when invoking as bash script?
I would leave the git-receive-pack symlink alone, instead create a bash function (in ~/.bashrc). Something like this should do the trick: function git-receive-pack() { ... (do your stuff) command git-receive-pack $* }

Categories : Linux

How to run a command within a bash script which gets it's arguments from a csv file
while read -r account abelpass entapass You are reading abelpass & entapass but passing password1 and password2 ! Didn't you meant to use while read -r account password1 password2 ?

Categories : Linux

detecting command not found in bash script
UPDATED Try [ -x "$executable" ] && echo "Command '$executable' not found" >&2 && exit 1 This will write an error to stderr and exit with 1 exit code. If You have just the name of the utility You can check its path with type build-in. Example: type type type ls type xls Output: type is a shell builtin ls is /usr/bin/ls ./test.sh: line 13: type: xls: not found Test returns 1 if utility not found. So if the $executable can be anything (a bash build-in, alias, binary, ...), then this could be used: type -p ls>/dev/null && ls -l type -p xls>/dev/null && xls --some_arg This will run ls (any executable), but not xls. Anyway if in the script the execfail option is not set (shopt) then the script will exit after stating the bash: some_u

Categories : Linux

How can I make a bash command run periodically?
If you want to run a command periodically, there's 2 ways : using the crontab command ex. * * * * * command (run every minutes) using a loop like : while true; do ./my_script.sh; sleep 60; done (not precise) See cron Some pointers for best bash scripting practices : http://mywiki.wooledge.org/BashFAQ Guide: http://mywiki.wooledge.org/BashGuide ref: http://xrl.us/bhk9ww http://wiki.bash-hackers.org/ USE MORE QUOTES!: http://www.grymoire.com/Unix/Quote.html Scripts and more: http://www.shelldorado.com/

Categories : Bash

Bash script execute command on multiple servers
ideally just put the commands in quotes like this: ssh admin@10.x.x.x '/sbin/arp -an | grep lanx' > lanx or ssh admin@10.x.x.x '/sbin/arp -an' | grep lanx > lanx The other problem might be the user admin on your machine does not have arp in PATH (is he root? arp is usually in /sbin/ and /sbin/ is usually not in PATH of a regular user.

Categories : Linux

How to debug a bash script and get execution time per command
You can use set -x to have the script print each command before it's executed. I don't know of a way to get command timings added automatically. You can sprinkle date commands throughout the script to mark the time.

Categories : Linux

creating bash script that runs for every subsequent command
If I understand your question correctly, I'd do the following: Create a script, eg mycommand.sh: #!/bin/bash while [[ 1 ]]; do read _INPUT echo $_INPUT done initialize an infinite loop for each iteration, get the user input ( whatever it is ) and run it through the command you specify in the while loop ( if your script needs to parse multiple arguments, you can swap our echo with a function that can handle that ) Hope that helps!

Categories : Linux

How can I pass a variable onto a command's parameter in a bash script?
you need to use array: #!/usr/bin/env bash BACKUP_EXCLUDES=() function exclude { while (( $# )) do BACKUP_EXCLUDES+=(--exclude="$1") shift done } exclude /proc /dev /mnt /media exclude "/lost+found" rsync -ruvz "${BACKUP_EXCLUDES[@]}" / /some/backup/path the bash explanation: please check @janos answer the zsh explanation (if it was Zsh): when you used a string variable it was like you passed --exclude=""/proc" --exclude="/media" ..." - so it was treated as long path with spaces - which never matched.

Categories : Linux

trying to execute a bash command in my apache server through my php script but getting error
You have to escape your double quotes in the string: exec("awk -F: '{printf "www.example.com/2/tasks.php?account=%s%%3A%s\n" ,$1, $2 }' < /opt/lampp/htdocs/$filename > /opt/lampp/htdocs/2.txt");

Categories : PHP

bash: get literal parameters passed to a script and execute them as a command
You are on the right track, and almost got it. You just need to use "$@" instead of $@. Here's a summary of what $* and $@ do, with and without quotes: $* and $@ paste in the positional arguments, then tokenise them (using $IFS) into separate strings. "$*" pastes in the positional arguments as one string, with the first character of $IFS (usually a space) inserted between each. "$@" pastes in the positional arguments, as a string for each argument. Examples: $ set foo bar "foo bar:baz" $ printf "%s " $* foo bar foo bar:baz $ printf "%s " $@ foo bar foo bar:baz $ printf "%s " "$*" foo bar foo bar:baz $ printf "%s " "$@" foo bar foo bar:baz Here's what changes when you set $IFS: $ IFS=: $ printf "%s " $* foo bar foo bar baz $ printf "%s " $@ foo bar foo bar baz $ printf "%s " "$*" f

Categories : Bash

Bash script TAR by Filetype is not gzipping the specified filetypes on recursive command
That's because xargs splits the huge list of files across multiple tar commands, which each try to create a new file. Try using the -A option instead of -vczf ... and pipe the whole command to gzip: find ... -print0 | xargs -0 tar -A | gzip - > "....tar.gz"

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++

How to Extract some Fields from Real Time Output of a Command in Bash script
It may not be sending any output to the standard output stream. There are several ways of sending output to the screen without using stdout. A quick google search didn't provide much information about how it works internally.

Categories : Bash

First line of the output of a command within a bash script overriding the exit status
function myfunc() { bash -x $1 | tee -a output.log } myfunc $1 if [ $?=0 ] then echo "Success" else echo "Failure" fi This will execute the script and check the return code

Categories : Bash

bash script to get command values in two files and write is a pattern to new file
To get the common lines, you can do something simple like awk 'NR==FNR{x[$1]=1} NR!=FNR && x[$1]' file1 file2 That leaves you with a list, and you need to group the elements in ranges. That's a simple awk script: awk 'NR==1 {s=l=$1; next} $1!=l+1 {if(l == s) print l; else print s ":" l; s=$1} {l=$1} END {if(l == s) print l; else print s ":" l; s=$1}' Putting it all together: awk 'NR==FNR{x[$1]=1} NR!=FNR && x[$1]' file1 file2 | awk 'NR==1 {s=l=$1; next} $1!=l+1 {if(l == s) print l; else print s ":" l; s=$1} {l=$1} END {if(l == s) print l; else print s ":" l; s=$1}' Explanation: We keep track of the start of the current range and the last value we saw. NR==1 {s=l=$1; next} NR==1 only runs on the first line. It will always be the first element of a range.

Categories : Bash

Timeout a command efficiently and capture output to a variable in bash script
Fortunately, the $() notation allows for multiple commands, so you can do this: output=$( ( my_command ) & pid=$! ( sleep $TIMEOUT && kill -HUP $pid ) 2>/dev/null & watcher=$! wait $pid 2>/dev/null && pkill -HUP -P $watcher ) You can also use regular () to group commands and then redirect all their output. Redirecting stderr to stdout can be done using 2>&1, so you end up with this: output=$( ( ( my_command ) & pid=$! ( sleep $TIMEOUT && kill -HUP $pid ) 2>/dev/null & watcher=$! wait $pid 2>/dev/null && pkill -HUP -P $watcher ) 2>&1 )

Categories : Linux

Bash script runs from successfully from command line but not when invoked by program
Following on the suggestion by @John3136, try putting the full path to aptbatch.sh, e.g. /home/scripts/aptbatch.sh, instead of just the name. If it's not in the $PATH of whatever shell is executing the script, it won't be found.

Categories : Bash

Rescue ruby command in shell script
to run system commands within ruby, you can do the following: system 'twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json' this gives you the global variable $? for the exit status of the process which should be 0 if the process didn't succeed. so your ruby script would be something like: system 'twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json' if $? == 0 #retry, throw exeption, etc. end you can also use backticks to run the command and get output status. the following may work for the timeout error. output=`twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json` ; result=$?.success?

Categories : Ruby

Bash script/command to bulk remove "@2x" from filename (retina image -> normal)
Bash offers substitution using the ${var/} syntax. Example: ${i/AAAA/BBBB} replaces AAAA occurrences in $i with BBBB. Therefore loop over all files matching *@2x.* and rename each one. for i in *@2x.*; do mv "$i" "${i/@2x/}" done

Categories : Bash

How to make a wget and mysql bash script
#!/bin/bash DBNAME=DB_NAME USER=USER PASS=PASS HOST=localhost STATEMENT="select name from environment;" COUNTER=$(mysql $DBNAME -h $HOST -u$USER -p$PASS -e "$STATEMENT") for entry in $COUNTER; do echo wget $entry done Here is the output ./mql.sh wget name wget Prod wget uat

Categories : Mysql

How correct call make in bash script?
exec is "Instead of the rest of the script, execute this command". Remove them both: # ...as before... cmake ../ make

Categories : Bash

Provide password to ssh command inside bash script, Without the usage of public keys and Expect
AFAIK there is no possibility beside from using keys or expect if you are using the command line version ssh. But there are library bindings for the most programming languages like C, python, php, ... . You could write a program in such a language. This way it would be possible to pass the password automatically. But note this is of course a security problem as the password will be stored in plain text in that program

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

How can I run a Ruby script from command line and get the response using Java code?
You can use this: String[] commands = {"ruby","D:/MyProject/myruby.rb"}; Runtime rt = Runtime.getRuntime(); Process proc; try { proc = rt.exec(commands); BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); String s; while ((s = stdInput.readLine()) != null) { System.out.println(s); } } catch (IOException e) { e.printStackTrace(); }

Categories : Java



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