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

learn more… | top users | synonyms (2)

2
votes
2answers
356 views

How to get the name of the nearest interactive ancestor shell from a script?

I know that one can use the $- contains i or not or check if $PS1 is empty to tell if a shell is interactive. However, those solutions only work for the current shell. I have a bash script trying to ...
1
vote
2answers
47 views

How to see what's going in background when we run a .sh file in Linux? [closed]

I know one way i.e. ./filename.sh > log.txt. But that log file is containing the content what appears in console if we run the file using ./filename.sh. Background process is not appearing in log ...
0
votes
0answers
27 views

Run script at startup in linux

I want to run my script at startup of my machine and decided to "echo /opt/script/threshold.sh >> /etc/rc.local".I know there are many other ways to do so,Please suggest which one is better and ...
1
vote
1answer
33 views

Catch number of syntax errors in shell script [duplicate]

EDIT: Question was marked as dublication. While this might be true on a larger scale, the answer providet by Kusalananda gave me a better understanding of my options. if a syntax error inside a shell ...
1
vote
2answers
41 views

How to print all subsets of a set?

How can I print all subsets of a set using bash script. like : {} , {1} ,‌{2} ,{1,2} for A={1,2} this is what i have already write but i always thinks there must be a better way also my script just ...
1
vote
1answer
1k views

How can I kill child processes with start-stop-daemon?

I'm writing a simple web server using nc and bash. It looks like this: #!/bin/bash rm -f /var/run/streamman/out mkfifo /var/run/streamman/out trap "rm -f /var/run/streamman/out" EXIT while true do ...
-2
votes
2answers
22 views

ignore row in calculation using awk

I'm writing a script which should calculate the sum of a column entered by the user. In the column, there is one row which is a date (dd/mm/yy). Now awk is doing the summation of all values, ...
1
vote
1answer
28 views

Initilization script runs twice on SSH

Running Ubuntu 12.04. I have a script that sets up the environment, its run by /etc/bash.bashrc. (It may be set to run by other shell profiles inits, I didn't actually set it up myself) When I Ctr+...
5
votes
4answers
313 views

bash + using printf in order to print in special format

I just wrote the following bash script to check the ping access on list of Linux machines for M in $list do ping -q -c 1 "$M" >/dev/null if [[ $? -eq 0 ]] then echo "($C) $...
53
votes
6answers
94k views

How can I test if a variable is empty or contains only spaces?

The following bash syntax verifies if param isn't empty: [[ ! -z $param ]] For example: param="" [[ ! -z $param ]] && echo "I am not zero" No output and its fine. But when param ...
0
votes
0answers
22 views

linux + get standard output from remote machines [on hold]

Is it possible to get the standard output from remote machines - 100 or 1 , after running the script /tmp/script.bash ? ssh $remote_machine /tmp/script.bash echo $? ( from remote machine script - ...
1
vote
2answers
815 views

Scp command over ssh without password in script

Here is my problem: I would like to perform scp command after ssh to remote server without typing any passwords. I describe my steps. My first steps were: ssh-keygen -t rsa and local@host$ ...
2
votes
1answer
131 views

How create a Init Script LSB Header for this script in Debian?

I have below script, but how can I create an LSB Header for it in Debian? I want to put in my init.d. I know that for Fedora is ok, but for Debian I don't know. Or where and how I put in rc.local? #...
19
votes
7answers
91k views

How can I monitor all outgoing requests/connections from my machine?

My machine is a server so I want to ignore connections being made to my server (e.g. when someone visits my website). I want to see only connections/requests being made by my server to other places. ...
1
vote
1answer
32 views

read the file and assign the values to another file

I want to read the file and assign the values to variables whic is in another file using shell script. For example I have file as "myconf" cli="/opt/Solartis_Monitoring/JON_CLI/bin/rhq-cli.sh" user="...
0
votes
1answer
19 views

How to check not starts with condition in if statement?

I want to check the condition like below. Line should not starts with # symbol if[ ! ["$Line" == "#*"] ]; then But this is not working.. somebody help on this plzzz.
-1
votes
0answers
43 views

print line which has atleast one common word in the line between two files [on hold]

A have two files: file 1: sample one i am new to unix need oracle query Also java program no idea about the script file 2: this is reference file have oracle with me expert in unix programming ...
17
votes
2answers
20k views
27
votes
7answers
32k views

how do I trim leading and trailing whitespace from each line of some output?

I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like trim I could pipe my output into?
22
votes
8answers
41k views

Test if there are files matching a pattern in order to execute a script

I am trying to write an if statement to test whether there are any files matching a certain pattern. If there is a text file in a directory it should run a given script. My code currently: if [ -f /*...
0
votes
1answer
23 views

How to print ${STR1} and $STR2 - not their contents - into a file?

I need to print expressions ${STR1} and $STR2 into a file -- not the content of the variables. How can I accomplish this using echo or printf?
1
vote
1answer
66 views

How to compare a program's version in a shell script?

Suppose I want to compare gcc version to see whether the system has the minimum version installed or not. To check the gcc version, I executed the following gcc --version | head -n1 | cut -d" " -f4 ...
10
votes
4answers
370 views

Run command after a certain length of time has elapsed?

If I'm executing a long process, is there any way I can execute some time-based commands? For example, I'm running a really long process which runs for roughly 10 minutes. After 5 minutes, I would ...
3
votes
3answers
41 views

Proper type casting in a shell script for use with while loop and modulus

I am trying to write a script to get a random, even hex number. I have found the the openssl command has a convenient option for creating random hex numbers. Unfortunately, I need it to be even and my ...
0
votes
2answers
43 views

Skip catching errors in a shell script

I have a script which compile a program. This program first compiles the source code using configure && make commands, then runs some tests using make test. This script also uses set -e to ...
0
votes
1answer
26 views

Shell Scripting - double condition with two variables

Good morning folks, I have to do this for my class assignment and I don't quite grasp how to. The script will ask the user for two separate statements which will fill two variables, L and C. I need ...
0
votes
1answer
29 views

how to keep oldest file in same directory

I had 2 directories "../in/" and "../temp/".My question is how to write bash script to keep the oldest file or directories in same ../in/ directory and remaining files or directories are moved to ../...
3
votes
3answers
2k views

Query list of servers for memory/cpu allocated

Running RHEL 6.2, trying to write a bash script to SSH to a list of remote servers, and write the CPUs and Total Memory to a file, one line per host in the following format: HOSTNAME1 CPUS: 12 ...
2
votes
1answer
37 views

redirect output of command to /dev/null

I run the following line from my bash script in order to run the script /usr/run.pl on remote machines xargs -P "$numprocs" -I '{}' -n 1 -- perl -e 'alarm shift; exec @ARGV' -- "$timeout" ssh -...
0
votes
1answer
25 views

Expect Rsync with Double quotes

Below is my code: #!/usr/bin/expect -f set timeout -1 spawn rsync -arvz -e 'ssh -p 1690' --protect-args --progress /home/pappu/ 'backup@xx.xx.xx.xx:/volume1/56 - Backup Server/pappu' expect "password:...
0
votes
1answer
32 views

command not found using sudo in script

I am trying to execute sudo command inside a bash script. #!/bin/bash sudo node app.js and it throws me : sudo: node: command not found If I try to execute only node app.js (without sudo ),it ...
0
votes
1answer
22 views

joint two conditions in bash script

Note: Update Jun 28 I have an iptables script .sh (bin/bash) with the rules: function run_ips(){ for ip in $(sed '/#.*/d' ips.txt); do if echo $ip | grep -q "-" >/dev/null; then iptables -A ...
2
votes
1answer
1k views

How to create gcov files for a project in a different dir?

I want to analyze code coverage datas. I want to create gcov files from OpenSSL (and from other projects), but I can only create them in the same directory of the project, and only for the files in ...
0
votes
1answer
22 views

Running an X application from a shell script via systemd timer

I'm trying to create a systemd (user) timer that opens certain tabs every day at 9PM with xdg-open. Here are the files: /home/me/.config/systemd/user/test.service [Unit] Description=Test [Service] ...
0
votes
1answer
25 views

update lines in a file

I have a file,where it has some fields.For example, var One = "fcb"; var Two = "abc"; How can I update/replace these lines using a script? I want to be able to do something like : echo -n "...
3
votes
2answers
36 views

Flocking a filedescriptor in a shell script

I thought this would give me uninterrupted begin-end pairs, but it doesn't: #!/bin/bash fun()( flock 1 || { echo >&2 "$BASHPID: FAIL: $?"; exit 1; } echo "$BASHPID begin" sleep 1; ...
3
votes
1answer
35 views

Executing multiple commands in init.d script

I have the following init.d script: #! /bin/sh ### BEGIN INIT INFO ...
6
votes
3answers
19k views

How do I get absolute path from using find unix command

How to get absolute path by using find command. actually I am running the following script:- find . -size +20M | while read a do i=$(echo $a | sed 's:/: :g') echo $a; j=($i) ...
0
votes
5answers
879 views

How to make the script automated to take password on its own?

I have a script that is doing sftp some files from one server to another, like inside the script script.ksh I have the below lines; sftp user@ip_address << EOF cd path put file1 bye EOF When ...
1
vote
2answers
319 views

cpulimit on a bash script which runs other commands

I have a bash script which runs other cpu-intensive commands . When I apply cpulimit on the bash script, the output of top shows processes for the commands inside the script still run without ...
0
votes
0answers
71 views

Run console printing command after tmux starts up

I'm starting tmux from zsh with the window split into two panels. The only issue I'm having is screenfetch starts up and prints to both windows. Ideally I only want it printing on the left window. I ...
1
vote
1answer
15 views

run pm2 from remote shell script

I am trying to write a script which ssh into a bunch of my servers and runs some commands to start my app. One of the commands (pm2) always says pm2: command not found. This is how I'm attempting this:...
3
votes
2answers
68 views

confused with awk and shell variables

I am trying to run a script that uses awk. There is no error it seems, but it doesn't create the file that I want. This is the script: #!/bin/bash echo "Number of domain:" read number echo "Domain ...
3
votes
2answers
46 views

Remove old files in a directory except files present in an exception file

I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory. I know the ...
0
votes
2answers
42 views

My first Shell that count rows in each file

I'm trying to run a shell script that counts the number of rows in each file, and if the number < 2 I need to move it to a different directory. shell: #!/bin/bash foreach i in (ls *.DAT) a=`...
0
votes
1answer
27 views

Unix command to return maximum float value out of two after subtraction

This is similar to question about getting the minimum/maximum of two numbers, but I'm interested to do the same with float numbers. So basically I'm trying to use shell arithmetic expansion using the ...
0
votes
4answers
57 views

How can I run a command periodically and indefinitely till it's turned off?

I want to run a command every several minutes until I turn it off. After some searching, I found lots of ways of doing the first half (running a command periodically and indefinitely), what about I ...
13
votes
8answers
10k views

Is there a unix command that gives the minimum/maximum of two numbers?

I was looking for a command to limit numbers read in from stdin. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, ...
-3
votes
1answer
34 views

text to columns in CSV

I am having data in CSV file as follows in single column: Input: Company;C Desc;Region;R Desc; H;Hindustan;13;Maharashtra; I;Unilever;28;Dadra; I would like to change the data Text to Columns with ...
0
votes
2answers
33 views

bash: Why are () causing error in script but not on the command line?

If I type this on the command line: _longopt () { case "$prev" in --+([-a-z0-9_])) echo foo ;; esac; } it works as expected. If I put it in a script (foo.sh) it ...