Questions tagged [shell-script]

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

0
votes
1answer
1k views

command /opt/bin/date --date '-15 minutes 2016-04-27T14:14:47' producing result many hours before stated time

I'm using the following command in an attempt to work out the time 15 minutes before the timestamp: /opt/bin/date --date "-15 minutes 2016-04-28T16:58:55" '+%Y-%m-%dT%H:%I:%S' The problem is the ...
0
votes
1answer
2k views

Capture Error of LS to file [duplicate]

#!/bin/sh if [ $(ls sample01.log | wc -l) = 1 ] then echo "File Found" > lsOutput.log else echo "File Not Found" > lsOutput.log fi But if sample01.log is not already existing my code already ...
0
votes
1answer
28 views

Add lines if they are missing from a data series

I have a data gathering device which records 5 attributes of data per second. The first column of the data is the time elapsed, in seconds, and is always sequential ie n+1. Data fields are comma ...
0
votes
0answers
30 views

Solaris - modified date file age [duplicate]

I have a bash script on a Solaris server to provide an alert if a file is in a folder location for longer than 5 minutes. It seems the -mmin command doesn't work in Solaris. if [ -f $1 -mmin +15 ] ...
0
votes
4answers
48 views

How can I do subdirectory manipulation in shell?

For example, running a command on n-number of sorted subdirectories, where n is an input. Or how can I run a for loop on a range of subdirectories where I can give that range as an input? Like the ...
0
votes
2answers
103 views

How to output result of a find command that excludes certain file extension

Here is my find command: find -mtime -$a | xargs zgrep -il ''$b'' | xargs zgrep -il ''$c'' | xargs zgrep -il ''$d'' | xargs zgrep -il 'ST.997' | sort -u This is an example output of the above ...
0
votes
1answer
759 views

How to touch everything in a directory including hidden, like directory up `..`?

This question has parallels to question "touch all folders in a directory". How to touch everything in a directory, recursively including hidden entries, like "directory up" .. and . without de-...
0
votes
2answers
469 views

Set yesterday Date in script special for Monday

I am writing a script a check whether exist a backup file every morning Monday to Friday. These backup file is saved in the end of everyday from Monday to Friday only named as 02_10_15 There is a ...
0
votes
2answers
4k views

A set of libraries like ncurses in a shell script

Supose you present the following TUI in the shell: I need a set of libraries that can be used in the shell to do it. Being sure, ncurses has not been used, because it make a dependecy. Question: How ...
0
votes
4answers
1k views

Rename file by its folder name

I have a sort of folders (A,B,C,D,E,F,G,H) and I have one file in every folder has the same name in all the folders (file.txt) but the size of this file is different from folder to folder. I wanted to ...
0
votes
1answer
2k views

Shell script to process service restart procedure

We're running a 3rd party TTY service on our servers, to support some thin clients manufactured by the 3rd party. From time to time, connection to a thin client can get locked up. Procedure is to ...
0
votes
2answers
7k views

Current time/date as a variable in BASH and stopping a program with a script

I am trying to create a raspberry pi spy cam bug. I am trying to make it so the new file created for the various processes come up with NOW=`date '+%F_%H:%M:%S'`; which works fine. but it requires ...
0
votes
2answers
2k views

map array of arguments and then convert to string

Say I have these bash functions in a script: foo(){ my_args_array=("$@") export my_args="${my_args_array[@]}" bar $my_args } bar(){ echo "number of args: $#"; } foo a b 'c d e' if I run ...
0
votes
2answers
880 views

Define a set of values for a variable in shell script

I want to define a set of values for a single variable. E.g. singleDigit={0,1,2,3,4,5,6,7,8,9} If the user gives 10 it should exit with an error. How can this be achieved without using a loop, but ...
0
votes
2answers
497 views

Iterate over some pattern using shell

I have a file to process and get some entries from it. The file format is: Example Input: String1:"Hello" String2:"World" String3:"Question" End String1:"HelloAgain" String5:"Answer" End String1:...
0
votes
1answer
88 views

netcat pipe running too early?

I have the following script running in the background: while true; do { file=$(/somescript); echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat $file; } | nc -l -p 20000 done It works fine, except that /...
0
votes
2answers
45 views

Keep only 3 lasted date files(i.e. Appended date in the files) in the set of files and delete remaining files in the particular directory

I have one scenario which I should need to implement in the Unix. In a directory, I would like to delete the files based on the appended date at the end of the file, these files are XML back files. ...
0
votes
1answer
2k views

parse first column of command output, get corresponding second column value

I am executing a command that outputs a table, as such: firstValue: a secondValue: b.y7 thirdValue: c-x-g 10:42 GMT Say I need the secondValue. I can't do this by row number, because sometimes ...
0
votes
1answer
224 views

Cron job for 7 days alone not followed by multiple weeks

I need to execute a cronjob to run once for 7 days alone. I have tried like this: 0 0 * * 0-6 myscript.sh It gets run for once a day and running every day of the week since I gave as 0-6. But I ...
0
votes
1answer
28 views

Reference the stdin sent from piped sender process

Say I have this: delete_lock () { if grep -q 'PATTERN'; then # some command here fi cat >/dev/null } node foo.js | delete_lock say the node.js process writes "foodog" to ...
0
votes
2answers
334 views

Run a local expect script on a remote server?

So I am trying to basically ssh into a server and run a local expect script on there. I tried to follow something similar to how you'd do a bash command i.e. ssh -A user@remote "bash -s" < ./...
0
votes
2answers
524 views

How do I grep to find a file only matching a string enclosed with wildcards

I have to find a file with only a given number and a string. Ito find 856 enclosed with wildcards *856*. e.g. I have this fileA.txt with thousands of numbers: 45423542354235423542 ...
0
votes
2answers
596 views

read file record by record and do transformation to the subsequent record based on above record and write into another file

Data file is fixed length file, and I want to read the file record by record and do transformations to the subsequent records based on the prior records (and write the results into another file). ...
0
votes
2answers
97 views

executing commands in shell and turnoff the default displays of commands in console [duplicate]

This is my script cd /opt/product/vinoth -- some operations -- cd - While executing this command cd - the old path get displayed in the console. is there is any way to block this from displaying in ...
0
votes
2answers
228 views

Race Condition for Shell Blocks in Bash?

Update: This behavior is observed on Windows Subsystem for Linux. It seems there are two issues we are dealing with here: Some bug/race condition internal to the system. This is incorrect, see ...
0
votes
1answer
50 views

stderr is being sent down pipeline but I don't want that

I have this: echo "$some_command" | bash 2> >(prepend 'r2g-stderr:' 'red') | prepend 'r2g-stdout:' 'yellow'; What I am trying to do: r2g-stderr: this is some stderr r2g-stderr: this is ...
0
votes
1answer
37k views

How do I use: cat << EOF [duplicate]

I am little confused and not sure, how to add the cat << EOF style to my script. Would really appreciate a little feedback and help. Here is my script !/bin/sh #This is a disk mailer script ...
0
votes
2answers
341 views

file command + how to view all results from file command

I use the file command in order to verify if file (PATH) is text/ascii file or encrypted file Because file command have allot of results ( small example in example 1 ) I want to get the all ...
0
votes
2answers
217 views

Its possible use special characters in a shell function name?

I have to create the function in shell script, that function name must contain the special characters. like >() { echo $1 $2 } Here my function name is >, If its ...
0
votes
2answers
878 views

Adding array entries to existing JSON document from plain text input

I have a situation where i have output of one command, like this, 192.168.1.84 192.168.1.85 i want to use this, and make changes in another file, i.e add this ip addresses one after another like in ...
0
votes
4answers
7k views

How do I compare numbers in bash?

I have got this script, but it is not working. It is because it is failing to evaluate the number comparison in the if statement, I think. #!/bin/bash { read __ WIDTH; read __ HEIGHT; read __ __ ...
0
votes
0answers
22 views

print out anything in between “function $argument” to “}”, where $argument is the function name, of a given file [duplicate]

Let's say, i have some functions defined inside my ~/.zshrc function foo() { # do some stuff } function bar() { # do # lots # of # stuff } I search basically for a way to print ...
0
votes
1answer
25 views

Different result for positional parameters expansion and custom array expansion

There is a difference in the output of commands that I don't understand, as stated below: INPUT="$@" echo "$INPUT ${#INPUT} ${#INPUT[@]} ${#@}" # outputs: a b c 5 1 3 arr=(a b c) echo "$arr ${#arr} $...
0
votes
3answers
3k views

Passing Variable from one script to another

How can I pass a variable between scripts? Example: I have two test scripts: -- test2.sh #!/bin/bash echo "this is test2.sh" echo "varname is $varname" echo end -- tes1.sh #!/bin/bash varname="...
0
votes
1answer
135 views

Bash add text on line script [closed]

Trying to get my script to work but it won't work. Here is my script #!/bin/bash a="* * * * * /home/server/scripts/check.sh" b="0 06,18 * * * /home/server/scripts/checks2.sh" sudo sed -i '28s/^/'$b'\...
0
votes
2answers
444 views

GETOPTS parse empty and nonempty args

I am trying to make script that has two switches -h and -d, -d having a mandatory number argument. After it there will be undetermined number of paths to file. So far, I have this, but the code seems ...
0
votes
1answer
7k views

How to parse XML to CSV with a shell script?

What is the ideal way to parse an .xml file into a .csv file that I could include with my shell scripts. I know bash can't do it but I'm unsure if I should try in awk or sed. I imagine a language ...
0
votes
3answers
3k views

Count files in a directory tree [duplicate]

For the consistency check of a backup program, I want to define a function which counts all files in a directory including all files in subdirs, subsubdirs and so on. The solution I am trying so far ...
0
votes
2answers
103 views

Override `cd` or other builtin functions more than once

Say I have this bash function: cd(){ builtin cd "$@" echo "do my own thing" } the problem is that if I override cd in another place, only 1 override will count, any other override will be ...
0
votes
2answers
1k views

Bash script error trying to find directory of script itself: unexpected end of file

Borrowing graciously from this StackOverflow answer, I want to find the directory a script is running in so I can load relative paths for it on login: The script is pretty small right now: SOURCE="${...
-1
votes
1answer
500 views

Shell script does not run java, wrong parameters

I wrote a program in Java, which uses the RXTX serial port, so I had to include an additional library. At first I ran it from SSH, and it was working properly. After that, I was running some test ...
-1
votes
1answer
49 views

Why does using array and for loop breaks line into 2? [duplicate]

Why does the following gives the line I am searching: grep '<appointment-id internal:ref=1.2.3/>' test.xml OUTPUT is <appointment-id internal:ref=1.2.3/> While the following breaks ...
-1
votes
1answer
65 views

Correct way to call a Bash function from all sessions

I use Ubuntu with Bash and I have a file hosting a script with 5 functions. Sometimes I want to call some function of these functions (but not all of them), directly from the CLI. I could say it's ...
-1
votes
1answer
405 views

How to automatically .tar a directory once it's moved to a folder [closed]

I need an .sh shell/bash script that will automatically tar a directory with the same name of the directory when I move the target directory into another directory (destination directory), So for ...
-1
votes
1answer
434 views

Combining Certbot with an Nginx server block maker

I have an Ubuntu 16.04 environment with a pure Nginx server and I use the following Bash script to automate creation of server blocks and corresponding (self-signed) SSL certifications. It allows me ...
-1
votes
1answer
262 views

How to sort and join files in linux with huge data and print blank values for the values which are not found

Consider the below files : file1: 1 2 3 4 file2: 2 3 6 9 expected output: 1 NULL 2 2 3 3 4 NULL NULL 6 NULL 9 I have tried paste file1 file2 | sed 's/\t/\0\t/g' | column -s $'\t' -t ...
-1
votes
1answer
478 views

How to write the bash script dynamic to print row and columns data and update the same data in the same file? [closed]

abc_hosts,pwd_host_id,pwd_host_id,hostname,ddd_status,dddd_status, start_hosts,,,,,,,,,,,,,,,,,,, ,1,o1,fhffhfh,1,1,fff,fdfd,172.33.33.33,172.30.30.12,172.30.30.11,oreere.dff,43,443343,1111,43435,...
-1
votes
1answer
432 views

I'm trying to prevent the execution of a command in BASH.. Doesn't work always [closed]

This is based on another question of mine I am trying to prevent executions of certain commands using a script. I got the script to work perfectly in interactive shells but for non interactive shells ...
-1
votes
1answer
61 views

Cant use while loop because it is taking time is there any way faster way to get the final file..in linux [duplicate]

cat file_1.txt 100 200 name 100 200 100 200 150 300 150 300 150 250 150 250 150 300 name final file should be 150 300 150 300 150 250 150 250 i am using this command cat file_1.txt | ...
-1
votes
2answers
93 views

First match if we are on the first line of input, if so print the whole line, else print the first word followed by a comma

for eg: CREATE TABLE MWWDATA.ACK997 ( AKTYPE CHAR(2) DEFAULT '' NOT NULL , AKNUM CHAR(9) DEFAULT '' NOT NULL ); CREATE TABLE MWWDATA.APREIDEXC ( EMPLID NUMBER(15, 0) DEFAULT NULL ); I ...