Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

0
votes
2answers
70 views

Monitor a logfile and execute different commands depending on different conditions

I have a logfile which I want to monitor and depending on which condition is met, different commands should be executed. I found a solution that comes close to this here. Unfortunately it doesn't ...
0
votes
2answers
192 views

Search the value from a dynamic file and pass to a variable in HTML [closed]

I have the following file below with the format and I want to pass the value to HTML BE64533-A0E1-4F98-A91F-02C1D0 column=ABC_Received:ABC_Structure_Type, timestamp=1439978656596, **value=ASCII** ...
3
votes
1answer
12k views

Shell script error (: not found [No such file or directory]) [duplicate]

I'm using following script on linux server. And which is giving: : not found [No such file or directory] This is the script: # ------------------------------------------------------------------ # ...
2
votes
1answer
412 views

n-factor SSH authentication for CentOS 7

How can I enable (and require) n-factor authentication for all SSH connections to a CentOS 7 server? I have read about Google Authenticator, which provides two factor authentication. But that is ...
1
vote
2answers
83 views

How to subtract lines, in reverse, only if greater than previous line

I am working on a shell script that will read the lines of a file, in reverse. I need it to subtract the previous line from the latter, but only if the latter is larger. If it's smaller, then I just ...
1
vote
1answer
158 views

How to use sed to replace keys in a file using a regex?

I would like to modify a file to replace keys, suppose these keys are in the file as: 42NM 52NM 23NO XNNM I would like to replace anything with NM, with the word "Okay". 1 #!bin/bash/ 2 3 if [ ...
-3
votes
1answer
54 views

bash error: Missing '))' [closed]

#!/bin/sh echo "welcome to salary calculator" echo "Enter basic salary" read basic dp=$(( basic / 2 )) da=$((( basic + dp ) * 35) / 100) hra=$((( basic + dp ) * 8 )/ 100) ma=$((( basic + dp ) * 8 ) / ...
1
vote
1answer
56 views

printing in awk

I have a file number.txt with following record. Number.txt: 123456,2711448,1,14-Feb-15 09:24:25,14-Jul-15 09:24:25,120,20150814163821,13-Aug-15 09:24:25,,,12-Sep-15 09:24:25,,,12-Oct-15 09:24:25,,,...
1
vote
2answers
332 views

Extract one file from multiple zip files

I have a folder tree which contains multiple .zip folders (approx 600) each containing four files. I wish to extract just one file from each folder and copy it out to a separate folder. To complicate ...
0
votes
3answers
116 views

find command behaviour inside for loop

I have following part of code inside bash script. for dir in $(find /etc/aaa -type d -maxdepth 1) do #my code done #my code if /etc/aaa directory is not present then i want to exit script with ...
2
votes
3answers
151 views

Grab the last executed command from history as a string and store in variable in bash

I'm trying to grab the last executed command, with options, and store it as a string to be written to a file. I've tried using fc but that can only give me the next to last command (for me anyways) ...
1
vote
3answers
81 views

Process text file - Extract data starting with number

I have a list of contact information I need to process in .txt format seperated by , . We only want to keep SIC codes that start with 65 which is real estate. The command should only check for the ...
10
votes
6answers
748 views

Remove lines from a file depending on lines found in another file

File file1.txt contains lines like: /api/purchase/<hash>/index.html For example: /api/purchase/12ab09f46/index.html File file2.csv contains lines like: <hash>,timestamp,ip_address ...
2
votes
4answers
158 views

zsh: map command to array

suppose you have an array a=(foo 'bar baz') is there a more obvious way to apply a command/function to every array element and save the resulting strings into another array than this: b=() for e in $...
2
votes
2answers
245 views

Batch rename according to number of pages in pdf file

I have a lot of papers in pdf files (and also other type of files in the same folder). I'd like to rename them using the pattern <current file name>[<number of pages>].pdf But after ...
0
votes
1answer
142 views

Combining the if-else-if condition to a logical expression

Is the following expression correct way to combine the if-else if statement? if [ $TO_MAJOR -eq 7 ] ; then if [ $TO_MINOR -eq 3 -a $TO_PATCH -lt 2 ]; then echo "AE port not supported" ...
6
votes
2answers
4k views

Get the date of last month's last day in a shell script

How do I get the previous month end date, based on processing date? Examples: Processing date = 15jan2015 Expected date = 31dec2014, Processing date = 10feb2015 Expected date = 31jan2015
0
votes
3answers
349 views

Store CPU utilization into CSV file

OS: Debian 8 x64 I'd like to log every 5 seconds the CPU percentage into a CSV file in the format below: YYYY-MM-DD HH:MM:SS,CPU% I'm not familiar with how to use the top program to calculate the (...
2
votes
2answers
182 views

Find differences in file and sorting file by key

So I have two language files, english and french. They are in this format: key=translation An example in English would be: ui.title=My Title And in French: ui.title=Mon Titre So I need a ...
1
vote
0answers
83 views

Bash completion function for vim 'scp://' Paths

For easily editing remote files with vim, I currently use a few .vimrc and .bashrc additions to my environment. The bash-scripting side of it "misuses" the available standard _scp bash completion ...
1
vote
1answer
110 views

Awkward awk exit code

I'm writing some awk scripts for data validation. These are intended to be selected and invoked by another proces which needs to see the exit code from the awk script to determine if the content was ...
0
votes
2answers
52 views

sed command to append different text at multiple occurrence of string

I want to use sed command to append text at multiple occurrence of string. For Ex: Hello world types="program" Mario types="Game" Hello world types="program" Mario types="Game" So at first ...
0
votes
4answers
531 views

If condition with ssh command inside

I'm doing a bash script to backup my computer to a local server. I need to compress the archives but I can't find a way to make this if condition work with an ssh command inside: if [ ssh user@192....
4
votes
3answers
244 views

How to remove space in all sub directories in shell script?

I tried following shell script to remove all spaces with underscores: find $1 -depth -name "* *" -print0 | \ while read -d $'\0' f; do mv -v "$f" "${f// /_}"; done If I have a directory /home/user/...
1
vote
2answers
81 views

Escaping a linux command

I'm trying to run a command from a script I wrote that basically replaces a string in file vzctl exec VZID sed -i 's/\/>/address="$IP\/255.255.255.0"\/\>/' "file.xml" The problem is the ...
8
votes
2answers
4k views

How to replace spaces in all file names with underscore in Linux using shell script?

I tried following shell script which should replace spaces from all xml files for xml_file in $(find $1 -name "* .xml" -type f); do echo "removing spaces from XML file:" $xml_file mv "$xml_file" "${...
4
votes
2answers
101 views

How do I write a script to automatically optimize all the PNGs in my MediaWiki images directory?

I have MediaWiki 1.25.2 installed on my 64-bit Debian 8.1 VirtualBox machine and I'd like to be able to upload the images/ directory from this installation to my DropBox. Being a cheapskate I'm trying ...
-1
votes
1answer
154 views

sed throwing sed: -e expression #(number), char (number): unknown command: `(letter)'

I wrote a shell script which reads some files and copy all the data from these files to other consolidated file. This happens multiple times and previously copied data will not be copied again. For ...
1
vote
1answer
40 views

Get the list of packages that are not installed on a Debian system

I am writing a script to print the list of packages which are not installed. #!/bin/bash declare -a LIST_OF_PACKAGES=("php5" "nginx") for i in "${LIST_OF_PACKAGES[@]}" do PACKAGE_STATUS=`...
-1
votes
1answer
21 views

How to select from 2 numbers in a script?

I=1 APPCNT=$(itemCount $APP_LIST ,) while [ $I -le $APPCNT ] ; do APPNO=$(itemGet $APP_LIST $I ) APP_List=6,9 APPNO=6,9 is coming instead of 6 only or 9 only. What can I use instead of itemGet?...
0
votes
1answer
84 views

tail file from remote server and nslookup on local

I need a script for resolve hosts. For now I run this script on local machine for i in `tail -F access.log | awk '{print $8}' | awk '{gsub("http://|/.*","")}2' | awk '{gsub("http://|:.*","")}1' | ...
1
vote
1answer
41 views

search and increase the values between the second tilde symbol and the 3rd tilde symbol in each line of a txt file

I was wondering is there any way to search and increase the values between the 2nd tilde symbol and the 3rd tilde symbol in each line of a txt file. Maybe vi can do this? For example, i have a test....
2
votes
2answers
261 views

Save list of echo outputs to .txt

I'm having a lot of trouble at work trying to save a long list of echo outputs as a .txt file on my desktop. I am using Bash in Yosemite 10.10.4. I am still very new to Bash so any help and tips are ...
0
votes
1answer
127 views

How to append extension to files with certain filename format

I have several files generated by a script but I neglected to have the script append a file extension. Each filename contains the substring k120 and no other file in the directory contains that ...
0
votes
2answers
303 views

Get number of files in each directory [duplicate]

I have a directory with a very large number of subdirectories (~800) that were generated programmatically. I want to get a count of the number of files in each of these subdirectories to check for ...
11
votes
4answers
719 views

Fast method of splitting string from textfile?

I have a two text files: string.txt and lengths.txt String.txt: abcdefghijklmnopqrstuvwxyz lengths.txt 5 4 10 7 I want to get the file >Entry_1 abcde >Entry_2 fghi >Entry_3 jklmnopqrs ...
0
votes
4answers
106 views

how to print quote character in awk

I have a file str.txt with the following sample records. 31,2713810299,1,11-Aug-15 19:52:10 32,2713810833,1,11-Aug-15 21:36:18 Now I want to print output with awk as below. cat str.txt|awk -F, '{...
-1
votes
3answers
193 views

Why does the builtin `test` fail with 'less than' (<) and 'greater than' (>)?

This code: #!/bin/bash if [ "4" = "a" ]; then echo "equal"; fi if [ "4" > "a" ]; then echo "yes 1"; fi if [ "4" < "a" ]; then echo "yes 2"; fi echo "exit" prints this three lines: yes 1 yes ...
2
votes
2answers
256 views

Odd syntax for ssh and bash to run command

I'm pretty new to shell/bash scripting and I'm writing a script on OSX to automate some rather tedious efforts. I'm trying to run a portion of my script on a remote host using ssh. There's a common ...
1
vote
2answers
417 views

Bash is not sourcing ~/.bash_profile on OSX

I just got this really weird problem. When I start bash, I get these two lines: -bash: fasd: kommandot finns inte -bash: brew: kommandot finns inte "kommandot finns inte" means command does not ...
1
vote
1answer
41 views

Have cron check if the bash script is executing

I have a bash script that users while do along with sleep to constantly monitor the connection state and take measures in case something goes wrong. The only thing that is not guarded is the execution ...
0
votes
1answer
90 views

How can I mount /home in a usb drive and keep the files?

I need to write a script that can mount /home in an USB drive at the boot and keep all the files in the USB drive after ejecting it. At every boot, if there is an USB drive plugged in, the script ...
4
votes
3answers
2k views

Log monitoring using shell script

I would like to monitor a log file for errors and then send an email to administrators. The log file contains data like below 11 Aug 02:30 Service1 restarted 11 Aug 05:35 Service1 restarted 11 Aug ...
0
votes
1answer
139 views

opening a terminal with a command on startup in debian

I have an application which is an object file (obtained from a c source code). When I run this application from the terminal it works fine. I want to run this application on system start up. Since ...
5
votes
2answers
84 views

Bash wrapper that will format the output of another program

Is there a wrapper or a method to force the output of another program (let's say 7zip) inside a bash script to be formatted in a given way, say, coloured or indented? Or even run in a separate small ...
0
votes
2answers
157 views

Display CPU cores usage in percentage without using top

I am using java to run a command on linux. Since top is an interactive command and need to press 1 to get the info of all the cpu(s) so I cannot use that. So I am using the following command to get ...
0
votes
0answers
36 views

find on bash script ignoring folders with brackets [duplicate]

I have a strange issue here using bash. In the following script (this is a simplified version) the find command should list all folders but it ignores some folders: /Home Made videos /Home made ...
4
votes
3answers
367 views

Get date of next Saturday from a given date

I want the date of the coming Saturday after a given date. For example, for $date1="30-AUG-2015", I want the result 05-SEP-2015. I have tried all the commands below with no success: samba@samba:~$ ...
0
votes
2answers
28 views

Assigning output of a Dynamic Performance View (v$asm_diskgroup) to a variable in Shell Script

I am trying to assign the output of select name from v$asm_diskgroup; to a variable in Shell . The script is failing because of special character '$' in the table name . Note : While i ...
-1
votes
3answers
327 views

Difference between two files

I have two files , say a & b. I want to compare both the files and get the differences in the third file along with the file name. Can anyone help me out with a simple command.