w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
bash script read line by line and echo to file
make some edits like this : iname = checktest while read line do if [ -z "$line" ] then echo "" >> ${iname}2.txt else echo "$line" >> ${iname}2.txt fi done < ${iname} ; it should work now , hope this helps ...

Categories : Linux

"echo -n" works fine when executing script with bash, but not with sh
From man echo: Some shells may provide a builtin echo command which is similar or identical to this utility. Most notably, the builtin echo in sh(1) does not accept the -n option. Consult the builtin(1) manual page. In bash, the Bourne-again shell, echo accepts the -n option whereas in sh, the Bourne shell, echo does not, so it simply echos everything you wrote, including the -n.

Categories : Osx

Bash script with telnet "connection closed by foreign host"
Your box may have access restrictions that make it disallow connections from the machine you're running the script on. As general advice: do quote $line to prevent nasty things from happening, as in: echo 'foo'"$list"'bar' (notice the double quotes); instead of telnet, which is meant more for interactive usage, use nc (netcat): echo 'stuff' | nc -n 192.168.1.82 49153; this may help with your connection issues, if your telnet is trying to be smart and negociate telnet protocol specific parameters before it sends your data through; since you only use one command, echo, to send data, you don't need to run it in a subshell, so the parentheses ( ) are not needed; when you have multiple lines to output, consider using cat <<EOF instead of echo (look up here-documents in the bash manual)

Categories : Bash

Bash: echo a variable containing program output, to a file
The animated line contains of a series of control characters that modify the cursor position on the terminal. The same effect is not achieved when the output is directed to a file; you merely get a record of all the bytes that were written to the terminal.

Categories : Bash

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

How to write a bash script that can parse through an XML file as input and remove certain elements from the file
Perhaps you could use php like this: running php script (php function) in linux bash And then you something like domdocument(http://php.net/manual/en/class.domdocument.php) to read and the right the xml. Of course this is assumes you have php installed.

Categories : Xml

Pass content of file in variable into bash script - without writing the file down
You need one more <: ./script -a <<<"$CONTENT" <<< is called a herestring, and takes the following string and passes it as the standard input.

Categories : Linux

How to use external file as a source of variables for Bash script file?
Save this to a file e.g. script.sh. #!/bin/bash declare -A A while read -ra __; do A[${__[0]}]=${__[1]} done < "$2" while read -r __; do for I in "${!A[@]}"; do [[ $__ == *"$I"* ]] && { V=${A[$I} __=${__//"$I"/"$V"} } done echo "$__" done < "$1" Then run bash script.sh aaa.txt bbb.txt. To get the output run it as something like bash script.sh aaa.txt bbb.txt > out.txt I prefer to use Bash this time since you have to quote non-literals in Awk. Another solution through Ruby: ruby -e 'f = File.open(ARGV[0]).read; File.open(ARGV[1]).readlines.map{|l| l.split}.each{|a| f.gsub!(a[0], a[1])}; puts f' aaa.txt bbb.txt Another way to modify file directly: #!/bin/bash A=$(<"$1") while read -ra B; do A=${A//"${B[

Categories : Bash

difference between executing script using bash file.sh and ./file.sh
If "file.sh" is not executable then ./file.sh will not work but bash file.sh will. If "file.sh" does not start with the line #!/bin/bash (or another path to a valid bash interpreter) then ./file.sh will not work but bash file.sh will. Basically, in order for a script to look like an executable file it must: Have execute permission. Start with the line #!/path/to/interpreter.

Categories : Bash

Bash: How to use a secret file for a script?
When i had to do something like that, i used expect and a wrapper script that would fetch a password from a file. I.e. in my password file i'd have something like root@192.168.1.10 ThisIsMyPass user@localhost thisIsMyOtherPass and then have the wrapper script get (it could be simple as grep "root@192.168.1.10" ~/.passwords | cut -d ' ' -f2) Im guessing there are more appropriate methods, but with this one you only need to keep your wrapper and password file protected and you can make your setup script public.

Categories : Bash

parsing an .kml file in bash script
With bash if your lines are in order: while read WHEN && read GXCOORD; do echo "$WHEN : $GXCOORD" done < test.kml Or perhaps do it this way: while read WHEN && read GXCOORD; do echo "$WHEN : $GXCOORD" done < <(exec grep -Fe "<when>" -e "<gx:coord>" test.kml) And also perhaps trim out the tags: while read WHEN && read GXCOORD; do WHEN=${WHEN##*'<when>'} WHEN=${WHEN%%'</when>'*} GXCOORD=${GXCOORD##*'<gx:coord>'} GXCOORD=${GXCOORD%%'</gx:coord>'*} echo "$WHEN : $GXCOORD" done < <(exec grep -Fe "<when>" -e "<gx:coord>" test.kml)

Categories : Linux

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

How to paste many lines to a file using a bash script?
I would use cat together with here-doc syntax for this: cat <<EOF > /etc/nsswitch.conf group: compat shadow: compat hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 ... EOF The statement above will overwrite or create the file with the contents between the first line and EOF. In the form above even variables like group: $group would be expanded by bash. If you don't want this, then use <<'EOF' (note the single quotes ' around the EOF)

Categories : Bash

Javac in bash script returns no such file
You are not passing the actual file to compile - instead you're passing the .java file as the classpath. Your actual command looks something like this: javac -classpath /some/dir/Program.java while it should be something like this: javac -classpath /path/to/library.jar /some/dir/Program.java I think you're missing the actual classpath somewhere. Most likely your script needs to be similar to this: #!/bin/bash CWD=$(pwd) CLSPATH=/path/to/library.jar SOURCE="$CWD/Program.java" COMMAND="javac -classpath $CLSPATH $SOURCE" eval $COMMAND java Program Also, I'm not sure what the purpose of the eval approach is. Why not just invoke javac? I would further modify the script to be: #!/bin/bash CWD=$(pwd) CLSPATH=/path/to/library.jar SOURCE="$CWD/Program.java" javac -classpath $CLSPATH $

Categories : Java

Edit a text file using a Bash script
try this: awk '/localhost> localhost>/{print $1 >"file2";next}{$2=$3=$NF=""}7' file this will output text you want, and generate a "file2" for the localhost> localhost> case. didn't test, I hope there is no typoes.

Categories : Bash

linux - bash script - test a file
Not precisely. -N does a comparison between the file's atime and mtime, which is not accurate on e.g. ext3 filesystems that are mounted relatime. You should either use the OS's file monitoring facilities or compare the mtime of the file directly.

Categories : Linux

How can I write into a particular line of a file in a bash script?
You could modify the line with sed. catcenter.dat sed -i "/CenterListName/ s/=$/= $(cat center.dat)/" AnalyzeClusterParameterFile$c Read the value first and add it to the second file with sed.

Categories : Bash

Bash script for regex file renaming
Pure bash based solution: for x in *.pickle; do [[ $x =~ ^([^_]+_[^_]+_)([^_]+)(_.+)$ ]] && mv "$x" "${BASH_REMATCH[1]}$((${BASH_REMATCH[2]} + 1))${BASH_REMATCH[3]}" done

Categories : Bash

Bash Script to copy contents from one file to another
You can remove files from the argument list as you use them, with the shift command: if (( $# % 2 )); then echo Please enter an even number of filenames exit 1 fi while (( $# )); do src=$1 dst=$2 cp "$src" "$dst" shift 2 done

Categories : Bash

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

Create answer file for bash script
This basic trick may work in your case: echo -e "y y y y n" | ./script or echo -e "yes yes yes yes no" | ./script but it depends on how your script handles the input stream...

Categories : Linux

Pause for password sftp bash script file
After months of looking for an answer I have finally found the solution. It was in a comment on an answer in some other thread I can't even remember. Hope this can help others out there. Your bash script should look like this and will connect to the sftp server, prompt the user for the password, and then execute the remaining commands. #!/bin/bash sftp user@server <<! cd /the/remote/directory lcd /your/local/directory put/get some.file !

Categories : Osx

bash script to search the words from a file into the whole server
If you have saved your patterns in the file /root/my-wordfile, then you can use the following to find "all" the files under /home grep -rf /root/my-wordfile /home -r would search it recursively and -f would say the patterns are in /root/my-wordfile(separated by newline) Or if you want to search files with specific extension or names, you could use find as below: For example if you want to search all *.c files find /home -name *.c -exec grep -Hnf /root/my-wordfile {} ; Here -H would list the file name, -n would display line number.

Categories : Bash

Read txt file and parse the values to bash script
The following would give you a list of packages for which you want the reports: grep "^packages" config.txt | cut -d= -f2 | tr ',' ' ' Based on this, you can loop for values in the list: filename="config.txt" for i in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do for file in $(find /home/user/ftpuser -maxdepth 1 -name "*.[ew]ar" -type f); do echo /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs ${i} - input ../ftpuser/ -output ../reports/ "${file}" cp "${file}" /home/user/ftpuser/scanned/ done done

Categories : Linux

How to save entire output of bash script to file
After the usage message, add the following line: exec > "$2" This will redirect standard output for the rest of the script to the file named in the 2nd argument. Then run using myscript 192.0.2.42 output.txt

Categories : Bash

How to solve "bash: script/extension: No such file or directory"
It's because this plugin is for old version of spree which was for Rails 2.x and this command syntax is deprecated by now. I think it would be better for you to use something more up-to-date, like https://github.com/nebulab/spree-subscriptions.

Categories : Ruby On Rails

bash script to get modified file on remote server
Change: if ["$(( $(date +"%s") - $(stat -c "%Y" $i) ))" -gt "259200" ]; then to: if [ "$(( $(date +"%s") - $(stat -c "%Y" $i) ))" -gt "259200" ]; then Spaces around the [ are required.

Categories : Bash

Using variables in bash script to set ini-file values while executing
Not quite sure whether this helps or not: This calls the program script five times: for n in 1 2 3 4 5 do ./program $n input.ini done Then in program, refer to the first parameter $n as $1. The second parameter input.ini is $2.

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

Hazel bash script or Applescript to sum numbers in txt file
this awk one-liner should do : awk -F'$' '{x+=$2+0}7;END{print "Total: "x}' file it prints the whole file, and add a line Total: 98 at the end.

Categories : Bash

Validating Empty column of .csv file in bash script
If the echo prints ${Upper} blank, probably it contains only a space or a tab, change the if this way if [[ "${Upper}" =~ '^[[:blank:]]*$' ]]; then echo "VAR is empty" fi

Categories : Bash

Bash script for file listing (html output)
Are you looking for this?: $ ls -1 INFO1 - INFO2 - INFO3.doc INFO1 - INFO4 - INFO6.doc l.sh $ ./l.sh <li> <h5>INFO1</h5> <p>INFO2</p> </li> <li> <h5>INFO1</h5> <p>INFO4</p> </li> The script: #!/bin/bash for i in *.doc do # split file-name into array IFS='- ' read -a array <<< "$i" echo "<li>" echo "<h5>${array[0]}</h5>" echo "<p>${array[1]}</p>" echo "</li>" done update If filename looks like this: First name blabla - Second part bla boa - third part, unimportant.doc Change the IFS-parameter to a single - instead. Make this change: IFS='-' read -a array <<< "$i" output: $ ./l.sh <li> <h5>First name blabla &l

Categories : HTML

Bash function - return parent script file path
As soon as the parent script starts export "`pwd`/$0" or so, into an env variable, say ORIG_SCRIPT, then in the function just use ORIG_SCRIPT. You need to do this as soon as the script starts because $0 may be relative to the PWD and if you later change PWD before you need the value of ORIG_SCRIPT, it gets unnecessarily complicated. Update: Since you know the pid by $$, you may get something from /proc/<PID>/cmdline but I don't know how exactly this one works right now.

Categories : Bash

Error - "There is no script engine for file extension .vbs" when using "Git Bash Here" in Windows 7
The problem is caused by associating .vbs files with Notepad++ (or in some cases, perhaps a different program). I was able to solve it by running Notepad++ as an administrator and removing the file association for .vbs files. If you're not sure which program is causing the problem, you can find out by searching for "Change the file type associated with a file extension" from the start menu. To fix the problem from there, make sure that .vbs files have Microsoft Windows Based Script Host set as the current default program.

Categories : GIT

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

How to runCommand() of a bash script with redirected input from file in Mozilla Rhino?
You need to pass the input stream in as the input property of an object. var inputStream = new java.io.FileInputStream("fileInput.txt"); runCommand("somescript.sh", { input: inputStream }); If input is not an InputStream it will be converted to a string and sent to the command directly. Similarly, you can add output and/or err properties to capture the command's standard output and standard error (documentation here).

Categories : Javascript

Bash Script to copy latest file and send the output to mailid
#!/bin/bash # req.sh filestr=`find /a/b/DROP -name "*.idx" -mtime -1 -print` if [ "$filestr" = "" ]; then missing="1" echo "DROP ZIP File missing for `date +%D`" else echo "DROP ZIP file exists for `date +%D`" fi filestr=`find /a/b/ARCHIVE -name "*.idx" -mtime -1 -print` if [ "$filestr" = "" ]; then echo "ARCHIVE IDX File missing for `date +%D`" elif [ "$missing" = "1" ];then echo "Copying from ARCHIVE `date +%D`" cp $filestr /a/b/DROP else echo "ARCHIVE IDX file exists for `date +%D`" fi Run the script as sh req.sh |/usr/sbin/sendmail -r sender@domain.tld recipient1@domain.tld

Categories : Linux

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

Breaking a big file down into smaller files on special char in bash script
Your post appears to be cut off, but from what I gather this script should help you get started. awk 'BEGIN{ FS="|" y=1 outputFile="/tmp/outfile" }{ for (i=1; i<=NF; i++) { tmpoutput=tmpoutput" "$i if (y == 1000) { y=1 print tmpoutput > outputFile tmpoutput="" } else { y++ } } }END{ print tmpoutput > outputFile }' inputFile

Categories : Bash

Start and stop logging terminal output to file from within bash script
You can use the braces to redirect commands to tee through a pipe #!/bin/bash # to terminal and logfile.log { echo "aaa" echo "bbb" echo "ccc" } 2>&1 | tee logfile.log # only terminal echo "ddd" echo "eee"

Categories : Bash



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