Skip to content

Tag: shell

Exit whole script from code block { }

I have the following script: But the problem is that the {} block is exiting and not the whole script. How can I make the whole script exit? Answer Add the following line after your tee command: test ${PIPESTATUS[0]} -eq 0 || exit ${PIPESTATUS[0]} Reference Link: https://stackoverflow.com/a/34386000/2357256

How do I classify files in Linux server by their names?

How can use the ls command and options to list the repetitious filenames that are in different directories? Answer You can’t use a single, basic ls command to do this. You’d have to use a combination of other POSIX/Unix/GNU utilities. For example, to find the duplicate filenames first: This means …

Pass Telnet Command to SSH in Bash Script

I’m trying to write a bash to first ssh into a server and then telnet from that server to a ElastiCache Redis endpoint. My Code is the following: telnet.sh I would like to call my bash script and have the user interactively be connected to the Redis Cluster so that the user can enter redis cli commands …

Bash: if statement always succeeding

I have the following if statement to check if a service, newrelic-daemon in this case, is running… The problem is it’s always returning as true, i.e. “New Relic is already running”. Even though when I run the if condition separately… … it returns 0. I expect it to do nothin…

How to specify two variables on bash command line?

For example, I can do this: But how can I do this? I cannot use this: I have to do it in one line Answer As far as I know, every Bourne shell allows any number of name=value pairs preceding the command to establish the environment. Bash certainly does.

Using variables on grep –q doesn’t produce founds

I need to extract entries from a log file and put them on an errors file. I don’t want to duplicate the entries on the errors file every time that the script is run, so I create this: And is run using: The first time that the script runs it finds the entries, and create the errors file (there are

Bash sort -nu results in unexpected behaviour

A colleague of mine noticed some odd behaviour with the sort command today, and I was wondering if anyone knows if the output of this command is intentional or not? Given the file: We are looking to sort the file with numeric sort, and also make it unique, so we run: The output is: Now, we know that the numer…

Function to search of multiple patterns using grep

I want to make a bash script to use grep to search for lines which have multiple patterns (case-insensitive). I want to create a bash script which I can use as follows: and it should get traslated to: I tried following bash script, but it is not working: The error is: Answer I think you can do a recursive fun…

Variable as Tar file name in SunOs 5.10

I am trying to get the following result in the 1.files archived as tar with current date as a part of the tar name. 2.files removed once archived. /usr/sap/ST1/POC/backtest/*.txt has 2 txt files. This works fine as the file name is mentioned as arc_2017-02-28.tar. But when I tried this: The output is an error…