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

learn more… | top users | synonyms (2)

2
votes
1answer
168 views

Bash: pair up two series of files

I have a number of files with this pattern: file1_foo.ext file1_bar.ext file2_foo.ext file2_bar.ext and so on. I need to "pair them" and put them in a file like this file1_foo.txt ...
12
votes
4answers
11k views

Why doesn't “sudo su” in a shell script run the rest of the script as root?

A sample script can be as below: #!/bin/bash sudo su ls /root When using ./test.sh as the normal user, instead run ls as super user and exit, it switches to root; and when I logout, it executes ls ...
2
votes
3answers
4k views

Compare multiple files

I want a bash-script to compare OR find differences for multiple files which are stored in different locations: Files under /tmp/test/ A.txt B.txt C.txt Compare with files under /home/oldtest/ ...
0
votes
1answer
98 views

Colon breaks the variable subsitution [closed]

I am trying to lookup some C functions so that I could debug while using strace. So I setup a bash function to look it up in firefox (or links), but the substitution falls apart with : and escaping ...
2
votes
1answer
162 views

How do I move .bash_profile away from home

I need .bash_profile to be synched with Dropbox. The easiest way I can think of is to "redirect" the shell to search for .bash_profile in a custom folder. So is that possible? Or are there any better ...
1
vote
1answer
280 views

How to explicitly load shell interpreter in a C program

I want to create an executable script which is not readable/writable However since an interpreter needs to read this file it is not possible to do so directly. Therefore I wanted to know if I could ...
1
vote
2answers
265 views

Merge chunks from multiple files

I need to merge chunks from a few but large files. Each line contains the file name and byte offsets. # file begin end foo/a 11970 12010 foo/a 22995 23035 foo/b 45090 45130 foo/b ...
2
votes
1answer
850 views

bash script , echo output in box

I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution. #!/bin/bash function ...
0
votes
0answers
26 views

Figure out which distro I'm using with Zsh script [duplicate]

I'm making a script that installs some basic needs of mine on my newly installed Linux. However depending on the Linux distro the contents and commands would be different. In Arch Linux I would use ...
3
votes
2answers
726 views

How to run "find -exec <script> {}\;

I have a script that changes the properties of the files for a folder. Here is the example tree: dir 1 --file 1 --file 2 --file 3 dir 2 --file 1 --file 2 dir 3 --file 1 ...
1
vote
2answers
95 views

How do i move a pieces of data between two words if the data has a % percent symbol at the begining?

I have a log file looking somtheing like : SWEs-elmPCI-A-01(config)# class AutoQoS-VoIP-RTP-Trust ^ % Invalid input detected at '^' marker. SWEs-elmPCI-A-01(config)# ...
0
votes
1answer
12k views

bash script that print cpu usage,diskusage,ram usage

As we know that for sysadmins uses commands top for cpu usage, free -m for memory, df -lh for hdd space. I want to cut just usage details of cpu, memory, and Hdd. I need to make three commands and ...
1
vote
3answers
91 views

How can i check for existence of color in my linux box?

I want to do something like this in my linux box: if [[ colors exist ]];then echo "some text in colors" fi my doubt is : i am using sed to put colors in log file for some text but if the linux box ...
7
votes
7answers
799 views

Slick one-liner to convert a list like “1: 2, 3, 4, 5” to “1.2, 1.3, 1.4, 1.5”

Let's say I have a file that looks something like this: 23: a, b, c, d 24: b, d, f 25: c, g and I want to get output like this: 23.a 23.b 23.c 23.d 24.b 24.d 24.f 25.c 25.g Of course it's not ...
0
votes
1answer
288 views

Can someone provide an xargs example piping mysql query data into another command?

Can someone provide an example for the command xargs? I want to do a mysql query to return the ID field of a column then feed that result into xargs into another command say mysql query delete. How ...
0
votes
3answers
1k views

How to create an interactive bash script to collect arguments for a command in linux?

I'm trying to create an interactive bash script where I can collect user input variables to be used as arguments in another command. Can someone provide me an example of this? Currently i want to do: ...
2
votes
1answer
81 views

How do I change the colour of all lines to red begining with a % symbol?

When I use sed -i "s,.*%.*,$(tput setaf 1)&$(tput sgr0),g" A.log the whole line vanishes and when I use sed -i "s,^%.*,$(tput setaf 1)&$(tput sgr0),g" A.log only the first character(%) ...
14
votes
1answer
2k views

When is double-quoting necessary?

The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of ...
3
votes
1answer
234 views

How does having /dev/fd make running shell scripts under sudo safe?

At the end of the man page for sudo, there's the following remark: Running shell scripts via sudo can expose the same kernel bugs that make setuid shell scripts unsafe on some operating systems ...
0
votes
1answer
373 views

Why does my TCSH prompt change after cd?

My .cshrc file contains the following: set prompt = "%{\033[0;32m%}%S%B\! <%~> :%b%s %{\033[0m%}" Each time I cd out of my home directory, the prompt formatting resets to display: 33 ...
0
votes
1answer
126 views

How given script makes executables in /usr/local/bin/

While doing project , I found a script that is bounced over my head. Can you help me to understand this script . Please explain this script step by step. SCRIPT if ! [ -f /usr/local/bin/XXX -a -f ...
7
votes
3answers
3k views

What's the difference between Ctrl-Z and kill -STOP?

When I run a command (make on a large project) from the shell, I can type Ctrl-Z to stop the process and return to the shell. Subsequently, I can run fg to continue the process. I'm trying to write a ...
1
vote
0answers
229 views

how to create a script to backup MySQL databases on separate files?

I need to create a script so to automate the backup process for my database server. Today there are 19 databases on the server and to prevent some schema is created and not contemplated by the script, ...
1
vote
3answers
727 views

Get separate used memory info from free -m comand

As a output of free -m command I got following. total used free shared buffers cached Mem: 2496 2260 236 0 5 438 -/+ ...
4
votes
2answers
719 views

Simple Shell script to log disk space

I'm working on automating collecting disk space on a group of servers. In a standard terminal, if I run: ssh server1 ssh server2 df -h Then I get the output of the command, just fine. ...
2
votes
2answers
2k views

Testing a string containing only spaces (tabs, or “ ”)?

My code below doesn't work: stringZ=" " if [[ "$stringZ" == ^[[:blank:]][[:blank:]]*$ ]];then echo string is blank else echo string is not blank fi Result: string is not blank # wrong ...
0
votes
4answers
786 views

How to get data count between two strings with multiple occurences in the same file

I have the data in my file as START-OF-FIELDS ID NAME DEPT END-OF-FIELDS START-OF-DATA 1|joy|cs 2|sam|ec END-OF-DATA START-OF-FIELDS ID NAME DOB DEPT ADDRESS END-OF-FIELDS START-OF-DATA ...
8
votes
2answers
7k views

How can I remove an element from an array completely?

unset array[0] removes the element but still if I do echo ${array[0]} I get a null value moreover there are other ways of doing this but if an element of an array contains spaces like below ...
3
votes
4answers
1k views

Replacing pattern after nth match is found on each line?

I have a file containing lines: india;austria;japan;chile china;US;nigeria;mexico;russia I want to replace all the occurences of semicolon on each line with e.g. ;NEW;, but starting from the 2nd ...
6
votes
5answers
3k views

Avoiding busy waiting in bash, without the sleep command

I know I can wait on a condition to become true in bash by doing: while true; do test_condition && break sleep 1 done But it creates 1 sub-process at each iteration (sleep). I could ...
0
votes
2answers
105 views

perl one liner + print VALUE in the end of the line

The following perl one liner checks if $CURRENT_VERSION matches $NEW_VERSION exactly, and prints it if it does. Example: CURRENT_VERSION=223.3.12.4.5.3 NEW_VERSION=223.3.12.4.5.3 DATE=17.3.2013 ...
1
vote
2answers
2k views

Xdotool using “DISPLAY=:0” not works in Crontab

I need to run shell script contains Xdotool codes in /home/z/Desktop/tempo/run.sh. I've tried many ways of DISPLAY=:0 but always not works. I've tried each of below codes, and not works: * * * * * ...
3
votes
1answer
270 views

Searching for Failed attemps in /var/log/secure

I'm currently writing a script to examine /var/log/secure file and report how many failed attempts there were that day. I need to output it in the following format: Date: 03/15/10 Time: 10:30 Number ...
2
votes
1answer
175 views

Script to send mail if there has been no entry in a log file for a certain amount of time

I am new to shell scripting. How would I write a script which can send an alert mail if there is no entry in the log file for more than 3 hours.
5
votes
4answers
17k views

Using the not equal operator for string comparison

if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] || [ "$PHONE_TYPE" != "CISCO" ] then echo "Phone type must be nortel,cisco or nec" exit fi The above code did not work for me, so I ...
1
vote
0answers
1k views

rc.local not being executed when os boots up

I have a script that needs to be run when os boots up. I added it to /etc/rc.local. It was working fine for sometime but now rc.local does not load at boot time. When i checked the permissions on the ...
0
votes
1answer
245 views

top not working

My below cronjob is not working. What am I doing wrong here?? */5 * * * * /usr/bin/top -n1 | head -10 >>/tmp/load.txt
1
vote
2answers
174 views

Turning standard output hyphen into a single command

I am working with scripts that output xml and I am using xmllint to format the output. Xmllint requires a source argument, so I use the hyphen to pipe standard output to it: $> script.php ...
2
votes
1answer
254 views

perl + combine exec command with perl one liner line

I have the following short perl script rename.pl. How can I translate this script to one liner perl syntax? And how to combine the perl one liner in find command? rename.pl #!/usr/bin/perl $op = ...
1
vote
1answer
114 views

How to parse this output and put it into an array?

i have expect script whose output looks like : I would like to extract all words like Gi/02/12 etc in an array or a list. VLAN Name Status Ports ---- ...
1
vote
1answer
401 views

How to safely put user typed password into a file?

Is there any way to put the password typed by user into a file (either in Unix or in expect). read -s -p "enter password" input echo $input > file Is the above code safe? I want to use that ...
1
vote
2answers
1k views

multiple background processes in a script

Suppose if i have a situation in which some files need to be copied and it takes long time so i would have to go for parallel processing of the file copy. for example it may look like this for i in ...
4
votes
4answers
726 views

Count lines matching pattern and matching previous line

I want to count the number of times that a role is successfully deleted. The problem is that one line of the log file will show that the script is about to delete the role: Prepare to remove role X ...
1
vote
2answers
107 views

renaming a directory and its subdirs without affecting their files

I have to rename directories under the root directory with prefix "ms_PF" and then rename their subdirectories with the modified name of the main directory with an extra "_", and then copy all ...
2
votes
2answers
2k views

How do I use an If-Else query based on the day of the week?

Problem: I need to check if today is Thursday and perform different actions based on the result of this condition. I tried two different approaches: Getting the Day Name: DAYOFWEEK=$(date +"%a") ...
5
votes
2answers
169 views

[ vs [[ : which one to use in bash scripts? [duplicate]

The zsh man page, in its section on test (aka [), explicitly advises against using it at all, and urges readers to use [[ whenever possible. The relevant section states: The command attempts to ...
3
votes
2answers
145 views

Concatenate block of lines with following block of lines

I am trying to process some text exported from lecture slides using pdf2text. The bullet points of some slides show up like this: title for the list - - - a bullet point text another bullet point ...
2
votes
3answers
3k views

Bash script; optimization of processing speed

I was wondering if there are general guidelines for optimizing bash script. a) For example, it is more convenient to write loops than lines of commands, but is it also faster to process for the ...
4
votes
3answers
618 views

Linux command line that will fail if any standard out is produced

Is there a simple utility which I can pipe output to on Linux that will: Return a success code if there is no output on standard out (and / or standard error). Return a failure code if output is ...
13
votes
3answers
5k views

How can one run multiple programs in the background with single command?

How can one run multiple programs in the background with single command? I have tried the commands below, but they do not work. nohup ./script1.sh & && nohup ./script2.sh & -bash: ...