Questions tagged [shell-script]
Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).
16,479
questions
0
votes
0
answers
466
views
kill -s SIGINT does not work as expected when launched from init.d script
I want to start some python script (with venv) at startup and monitor the status, for auto restart it if something goes wrong.
I've made:
- a simple python example prg.py that sleep forever and exit ...
9
votes
4
answers
8k
views
Bash Read: Reading comma separated list, last element is missed
The output of the command below is weird to me. Why does it not give me back element 5?
$ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
0
1
2
3
4
I would expect '5' to be returned as well. ...
2
votes
1
answer
534
views
How to modify this `while read; mv printf` code to rename only files of certain filetype?
Code in question:
ls | cat -n | while read n f; do mv "$f" `printf "video_%03d.mp4" $n`; done
The above code will rename all files/folders within the directory executed to:
video_001.mp4
video_002....
0
votes
1
answer
94
views
Restore function in unix
I have a script to recover files back to their original location. I am missing the options for the script to as "Do you want to recover this file?" Yes or No. How will I implement it into this code ...
5
votes
1
answer
485
views
How to pipe multiple results into a command?
I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id ...
1
vote
0
answers
240
views
How to enable Python automatically after restarting my Centos 6.5 machine?
I am using Software Collections Repository (SCL) to install Python 3 package "rh-python36" on Centos 6 machine. I am adding this script to .sh file located in the /etc/profile.d directory:
#!/bin/...
1
vote
1
answer
205
views
How to move lines containing specific text to top of file in same order as code executed?
How to move lines containing specific text to top of file in same order as code executed?
Here is the code I have:
printf 'g?_001\|_002\|_003\|_004\|_005?m0\nwq\n' | ed -s target_file
( credit ...
0
votes
1
answer
4k
views
Jenkins how to catch error in script
What I am trying to do is once source .... command is executed if there is an error I want to put it into temp file. I tried several ways but no luck.
def responseValue = sh(script: """#!/bin/...
1
vote
3
answers
1k
views
How to move lines containing specific strings to top of file?
How can I move lines containing specific strings (ie. 005, and 007) to the top of the target file?
Contents of target file:
XXX_008
XXX_001
XXX_002
XXX_009
XXX_003
XXX_006
XXX_005 ----> located ...
0
votes
1
answer
51
views
Find files and specify some to come first in playlist, and the rest in random order?
The following code creates a list of the files in the directory and then shuffles them in a random order then spits out the file in a text document called list.txt
find *.EXT | sed 's:\ :\\\ :g' | ...
1
vote
1
answer
448
views
wget -i notifying when each file finishes (for processing purposes)
I'd like to be able to process multiple files downloaded by wget -i immediately after they are downloaded (instead of waiting for all files in the list to finish--for the entire wget process to exit). ...
0
votes
1
answer
266
views
How can we alert when EFS is unmounted for Linux EC2 servers?
I am using below script in cron job to get alert whenever EFS got unmounted.
But its not alerting me when the EFS is unmounted.
#!/bin/bash
Hostname=$(hostname)
##email subject
subject="EFS is ...
1
vote
3
answers
84
views
read file and based on criterion print desire output
I have input file which looks like this:
H1
O1
C1
H2
H3
O2
C2
Br1
I want to have an output file which to have the following output
H1 = 1
O1 = 8
C1 = 6
H2 = 1
H3 = 1
O2 = 8
C2 = 6
Br1 = 35
So when ...
1
vote
2
answers
183
views
Listen to a folder and modify incoming files on change
I want to convert pcap files to json files using shell script (and tshark). As soon as one or more new pcap files are copied into a folder, the command tshark should be executed and the resulting json ...
1
vote
0
answers
59
views
Is there a way to detect a sound above a certain volume playing through a device and react to it with a command?
I'm doing some scripting for automating a game and I need to click the mouse button and run some bash code every time a loud sound is played in the game. Is this possible with bash?
(Pop OS 19.04)
4
votes
1
answer
9k
views
Copying contents of a folder via SCP results in `scp: error: unexpected filename: .`
I was using a script to copy the contents of a folder via SCP, without copying the folder itself. Something like this:
scp -i id_rsa -P "$PORT" -r "$HOST:/folder1/folder2/." "backup"
(I'm not able ...
0
votes
2
answers
189
views
Concatenate the Content of Files from Various Directories with a Blank Line in Between
I have the file dir1.txt that contains the names of the following directories:
2
3
4
Directory 2 contains files 2_1.txt and 2_2.txt
Directory 3 contains files 3_1.txt and 3_2.txt
Directory 4 ...
2
votes
0
answers
71
views
bash script to output 15 lines or more of logs to a file if they occur in a 15 minute time frame
I am looking to create a script that will run daily via a cron job that:
output 15 lines or more of logs to a file if they occur in a 15 minute time frame and contain the keyword 'disconnects'
email ...
1
vote
1
answer
1k
views
cp runs from command line but file not found from script [duplicate]
I have a script to reorganize my directory structure that contains:
files=./*"$pattern"I{"$ifirst".."$ilast"}.ext
cp ${files} ../"$pattern"/"$i"/
When I run the script, I get an error:
./*S6I{001.....
0
votes
1
answer
217
views
How to start my application after starting of "daemon" process in single script
I have a script, that starts my server. I want to start openvpn client before it. Here my current working solution:
#!/bin/sh
openvpn --client --config $OVPN_CONFIG &
sleep 10
npm start
I ...
1
vote
1
answer
746
views
How do I confirm a file sync has completed before executing a command?
I have a computer periodically syncing folders of content with another computer using Resilio Sync. The receiving computer has a sorting process on an hourly cron, which analyses the folders and their ...
0
votes
2
answers
98
views
How to remove repetition of the content of file by column?
I have a file containing the following content:
WP_021815421.1 OG5_132922 deth|YP_182312 3 -07 42.35 81.7
WP_021815427.1 OG5_128110 rbal|NP_868155 1 -55 50.72 53.2
WP_012456834.1 ...
1
vote
1
answer
1k
views
Writing a Shell script to tail and gzip a log file
I'm trying to make a script that can tail log files from a remote server to my local directory. tail -F is what I'm using but after piping it with gzip, nothing happens although a local copy of the ...
2
votes
1
answer
2k
views
POSIX and Portability | shell scripts | grep -s, grep -q
I am all in for portability in regards to shell scripts.
But I am unsure if I am overdoing it right now.
In this example, we have a function called confirmation, which accepts the very first ...
1
vote
1
answer
611
views
Shell script to test ls output for directories
I am trying to make a BASH script to test the files in the working directory and return the files that are directory files.
One approach that I can think of is, starting with ls and feeding the ...
2
votes
2
answers
728
views
How to insert a name of the file as a header of a file?
Example: File name: ENSG00000000003
ENSG00000000003 43120.829491094
ENSG00000000005 39604.4956791524
ENSG00000000419 7645.05624570546
ENSG00000000457 2157.49855156382
ENSG00000000460 3317.98417717746
...
2
votes
0
answers
1k
views
PAM configuration to execute a shell script on every login failure
I would like to execute my shell script on every login failure. To do this I updated the file /etc/pam.d/common-auth: From (original body):
auth required pam_env.so
auth required ...
2
votes
4
answers
8k
views
Grep value of a specific key from a String, concatenated of key : value pairs
I have a string which is concatenation of "key":"value" pairs separated by "," like-
KEY1:VALUE1, KEY2:VALUE2, KEY3:VALUE3
From this string, I have to grep for a specific string -- let's say KEY2 -- ...
0
votes
1
answer
683
views
how to run a bash that must be run with a specific user with another user without password
I have 2 users. test1 that i created it with /sbin/nologin and has no password. test2 that has sudo privilege and has passworsd.
I have this script: /data/tempWork/fazitst that must be run under ...
0
votes
1
answer
765
views
How to make a full bash script that compares the shadow and the passwd file?
I have two files, passwd and shadow
a. Sort both files
b. Compare both files line by line
i. Match on user name output to third file
ii. If it doesn’t match, output to fourth file
iii. ...
1
vote
1
answer
67
views
How can i use my find in an if else statement to find duplicate files?
I want find duplicate files using my find, i cant seem to to make it echo "bububu" since it keeps echoing "blablabla" even if my path and name are wrong and there is no such file in that directory. Im ...
2
votes
1
answer
443
views
Create persistent array in bash
I want an associative array in my bashrc file and I want to add to or delete from it whenever needed, but if I put the array declaration in the bashrc file it will get redeclared every time bash is ...
-3
votes
1
answer
340
views
Script to prompt for the number of users; check how many files they have in their home directory and send a list of those users to the standard output [closed]
My homework asked me to
The users are storing way too many files in their home directory and you would like to notify the top 5 offenders. You might want to run this script again for more or ...
4
votes
1
answer
3k
views
Run script for 10 times or until it meets the condition
I have following shell script.
OUTPUT=$(systemctl is-active etcd)
if [[ $OUTPUT == active ]]; then
echo "The result is successfull"
else
echo "The result is unsuccessfull"
fi
I ...
0
votes
1
answer
1k
views
How can I pass an argument in a shell script without quotes?
I have to make my own command shell where a bash script calls the shell executable and does so with quotes. The problem is that when I try to do so with logical operators it seems that the quotes turn ...
0
votes
1
answer
1k
views
How to compare user input with content in text file?
I am making a script that allows users to save their settings in a text file. If they want to use the saved settings, they will need to input the name of the settings. Then the script will compare the ...
-1
votes
1
answer
645
views
Does execvp() account for logical operators?
In my class assignment I'm supposed to make my own command shell. In the shell, I am supposed to have it take various command line arguments including logical operators such as && and ||. I ...
-1
votes
1
answer
81
views
How can I run only a portion of a command line in a user built shell?
I make a shell that takes arguments in a command line with special conditions on what it is able to do.
For example, anything that appears after the # character will be considered as a comment and ...
0
votes
1
answer
155
views
Fork urxvt in bash without hitting Control-c
I know I can create new sub-shells (please correct me on terminology) in bash by using &. However this requires me to hit Control-c to get back to bash with most programs. Is there a way to stop ...
1
vote
2
answers
586
views
Bash recursive call fails
I am writing a function in ./bashrc to switch environment variables, commands, etc. This function is supposed to set up the necessary variables then call itself. Instead I get
bash: ./bash: No such ...
0
votes
2
answers
2k
views
Bash needs to send a command to another CLI
I want to automate some command the thing is we login to the server and the next commandline isn't a linux commandline but rather that of an application it self. My current attempt of solution is the ...
3
votes
2
answers
2k
views
PIPESTATUS from commands in $(...|...)
How to read ${PIPESTATUS[0] of a command, when a variable is set in current shell? Is there a way to pass it to current shell somehow?
I am setting variable MAIL=$(ldapsearch.... | find_for_mail) and ...
2
votes
1
answer
2k
views
Pass script to background using script itself
It is possible to do something like this:
nohup ./run.sh > /dev/null 2>&1 &
Which sends the process to background, discards its output and allows us to close terminal.
Is there any ...
0
votes
1
answer
3k
views
Install dependencies for a bash script
I've recently made a bash script that can be downloaded and installed by users via a Makefile. The problem is that I'm not sure how to have dependencies get installed. In this case, the only ...
-1
votes
1
answer
32
views
Value addition from a file's row [duplicate]
Suppose I have one file like:
apple | red | 2
apple | green | 3
orange | yellow | 3
apple | yellow | 1
Now I need following output as (irrespective of color value should get third number ...
1
vote
2
answers
365
views
Non-Root User SSH Privileged Command with File Piping
I have an a SSH server where root login is disabled. I'm hoping to run a privileged command that will want input from a file I have after being executed. I'm hoping to do all without user interaction ...
1
vote
1
answer
603
views
bash next month error
Running into errors. I am trying to get the next month when a user sends a date in.
D="2019/12/01";
D=$(date -d "$D");
echo "Current Date = $D";
prints Current Date = Sun Dec 1 00:00:00 EST 2019
...
0
votes
1
answer
229
views
For loop if statement syntax error
I have syntax error in my code to move files if the statement is true.The code goes on forever.Also tried adding quotes but unable to.I know there would be some silly mistake that I am doing.
for ...
0
votes
3
answers
270
views
How to add a modular bash script to `bin`?
I have a bash script that uses source to make the script more modular. Here's how it would look copied into a user's bin directory:
/bin
modules/
script-1
script-2
script-...
0
votes
3
answers
339
views
Add data to a particular line in a file
I need to build a script that adds comma separated values into a particular line in a file.
Say I have a file with contents:
DL1 : [email protected],[email protected],[email protected]
DL2 : [email protected],[email protected],tuy@kk....