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

learn more… | top users | synonyms (2)

1
vote
2answers
2k views

What does exec $SHELL -l do?

I have seen this command in a few different shell scripts: exec $SHELL -l ...usually as an alternative to: source ~/.profile What exactly is exec $SHELL -l doing?
2
votes
2answers
319 views

variable declaration does not work

I have the below script in my system: #!/bin/sh LASTFILES=`find /var/log -mtime -1 -iname *.csv` KEYWORDS="MD AG GA" countMD="0" countAG="0" countGA="0" for i in $LASTFILES ; do for a in ...
4
votes
2answers
227 views

Asynchronous RPROMPT?

A friend of mine posted this on StackOverflow, and I thought we might be more likely to get an answer here. His post refers to speed, but we now know that the python script we are using to parse git ...
7
votes
2answers
597 views

Possible to use brace (permutation) and array expansion simultaneously?

Bash's {} brace expansion syntax allows for creating easy permutations # echo {b,c,d}{a,e,i,o,u} ba be bi bo bu ca ce ci co cu da de di do du However it's not clear to me if/how it's possible to ...
4
votes
3answers
2k views

Summing up an array inside of awk?

I have the following piece of code: sum1= sum2= declare -a a echo $temp | awk '{split($0,a,","); name=a[1] ; for(i=2;i<=4;i++) sum1+=a[i] ; for(i=5;i<=7;i++) sum2+=a[i] }' This code is ...
2
votes
2answers
1k views

squid access.log delete entries older than 6 months

Squid generates a log of the accessed sites by the users. I want to delete the entries in the /var/squid/logs/access.log, which are older than 6 months. I would execute the script each week. I have ...
1
vote
0answers
36 views

(standard_in) 1: parse error [duplicate]

This is my script: #!/bin/sh # 5. Write a Shell Program to Find the Area & Circumference of a Circle echo "Enter the Radius" read radius arear=`echo 3.14 \* $radius \* $radius |bc` circu=`echo 2 ...
5
votes
5answers
821 views

How to concatenate two files on the fly and reference result as new file?

I want to concatenate two files, i.e. staticEntries.dic and dynamicEntries.dic. As the names might show, the content of staticEntries.dic stays the same over time (contains common words), but to the ...
1
vote
3answers
23k views

Returning a value from a bash function

I have a function which returns 1 if the number is a valid ten digit number: valNum() { flag=1 if [[ $1 != [1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]; then echo "Invalid ...
2
votes
1answer
833 views

Send attachment to gmail id from shell script

I have a shell script that generates reports. Using crontab i run the scrip everyday at 1000 hrs. I want to mail that report to my gmail id as an attachment. I have tried using mutt but it doesnt ...
1
vote
2answers
670 views

Improve sed command to replace first instance of character and all following characters?

Input: <e1 name="file1" id="id1" anotherId="id2"> Desired output: file1 I can get what I need with this: echo '<e1 name="file1" id="id1" anotherId="id2">' | sed ...
7
votes
5answers
6k views

Compare two files with first column and remove duplicate row from 2nd file in shell script

I will ask my question with an example. I have 2 files: File #1: 118D FC300_R5_TP FX.B 32775 2112 6 2038 6 2112 0 118E FC300_R5_TP FX.B 32775 2136 7 ...
2
votes
4answers
2k views

Shell script to validate mount point status in the linux server

I'm in need of a script which should work as below. A script where it would validate if that specific mountpoint is currently mounted on the server. It would search for mountpoint name in /etc/fstab ...
1
vote
2answers
188 views

How to write script that watching directory for lessc command?

I use lesscss extensively. I want to write bash script that watching my target directory, and then when .less file was modified and saved, command: lessc should be executed automatically. For ...
0
votes
3answers
221 views

Run two commands on same argument

I was wanting to maybe alias a command such that the command ran as per usual but also did something else on the argument/s before it was executed. An example might be: alias ls="echo displaying ...
1
vote
1answer
161 views

why csv is consuming more file size in my unix script?

I'm using this unix script which it is fetching records in the form of csv. I have a SQL query used for this purpose. It contains below information SET PAGESIZE 5000 SET COLSEP "," SET LINESIZE 2000 ...
4
votes
4answers
1k views

Joining two files with unique identifier

I have two files with approximately 12900 and 4400 entries respectively, that I want to join. The files contain location information for all landbased weather observing stations around the globe. The ...
2
votes
4answers
4k views

How can I either encrypt or render my shell script unreadable?

How can I encrypt or scramble my shell script so that it's unreadable to the naked eye? Either method would be acceptable, please provide specific steps.
3
votes
1answer
195 views

How can I write a sed script to delete numbers from a line?

Sample File a.b.gg3d 0000000001 0000000001 y a.b.112a 0000000000 0000000001 y a.b.gg3s 0000000001 0000000001 y a.b.1111 0000000000 0000000001 m I have a file that includes about three thousand ...
2
votes
2answers
781 views

How can I add an array of arguments inside my rsync call inside a script?

I want to copy a folder to another location, while excluding some specific files Here is my current script: #!/bin/bash if [ -n "$2" ] then source=$(readlink -f $1) destination=$(readlink ...
2
votes
1answer
5k views

bash: integer expression expected

I have a file out.csv I have to check if the name inputed by user exists in the file(comma separated) or not. I am taking name using read but while checking for equality I am getting error ...
2
votes
2answers
122 views

selecting specific string in a field

I have this file: N104 F1810 N104 F3810 N104 F810 N104 F305 and a script to grep an input. Script: enter input read input cat file | grep $input when I input “810”, I get the ...
2
votes
4answers
2k views

SSH exits after quit case in bash script

So I have the following standard script: #!/bin/bash PS3='Please enter your choice: ' options=("option 1" "Option 2" "Option 3" "Quit") select opt in "${options[@]}" do ...
2
votes
2answers
3k views

For loop for renaming files with prompt for each filename

I have to convert multiple video files in a folder. I have to rename each one of them but I need the script to prompt me to write a custom name for each filename. Here is what I've got so far: First ...
1
vote
2answers
437 views

Shell: Break both if and for loop at once

Shell: Break both if and for loop at once. My script: ip to name resolve code IP=192.168.27.191 hostNameChecker() { if [ `getent hosts $1 | wc -l` -ne 0 ];then HOST_NAME=`hostname` ...
2
votes
0answers
1k views

Script stops command in it with exit 0

When I write a bash script similar to this on RHEL 6, if [ "$2" = "" ] ; then echo vip.start: ERROR: no NIC specified echo vip.start: Need IP and base interface name exit 1 fi case "$1" ...
3
votes
3answers
12k views

Loop over a file and read values from two columns into variables

I have a file (data.dat) which contains 3 columns. I want to read in two of them with the shell script and fill the contents into a template file. The file looks like this: 12 180 2390 14 177 2210 16 ...
2
votes
2answers
2k views

what is >> symbol and >& in unix/Linux? [duplicate]

I have a CRONTAB entry as below. Can someone tell me what the below statement is exactly doing? 1 0 * * * /vol01/sites/provisioning/MNMS/45627/45627.sh1 >> ...
0
votes
1answer
334 views

Change date after SSH login in shell script

I have to change the date after an SSH login into machine, but I am not able to change it. Here is the script I have written: #!/bin/bash ENVIRONMENT_LIST=environment_ip_list USERNAME=root declare ...
3
votes
2answers
2k views

How to safely reverse dns lookup on ip in a shell script

I'm trying to find out how to take in a hostname and pop out an ip address (reverse dns lookup). I came up with this but I'm not too sure it's safe across different linux distros: nslookup $ip|grep ...
1
vote
1answer
1k views

How to figure out where a link gets redirected

I currently working on a script that updates a file. The file has several versions. The important ones have static links. These get redirected to the actual file which then gets downloaded using wget. ...
1
vote
2answers
2k views

To configure script to send email only when diff command gives a difference

I'm new to the world of scripting and and currently using a script to perform a health check on the san devices that I manage. The script is simple and redirects output of commands to a current file ...
1
vote
3answers
576 views

Which cmd is the best for determining the OS' word size (32/64)-bit? [duplicate]

I need to find my OS (not hardware) is 32-bit / 64-bit. Which command is best? uname -p uname -i uname -m arch All the above commands returns the same answer: On 32 bit systems: i686/i386 On ...
13
votes
1answer
1k views

How does a fork bomb work?

WARNING DO NOT ATTEMPT TO RUN THIS ON A PRODUCTION MACHINE In reading the Wikipedia page on the topic I generally follow what's going on with the following code: :(){ :|:& };: excerpt of ...
2
votes
1answer
190 views

Linux: I need to create installer for my own application using shell script

Linux: I need to create installer for my own application using shell script. I need to distribute my application as installer without GUI and also not as rpm's or any other packages. My application ...
17
votes
1answer
14k views

Empty the contents of a file

I am aware of three methods to delete all entries from a file. They are >filename touch filename filename < /dev/null Of these three I abuse >filename the most as that requires the ...
5
votes
3answers
1k views

Cron to not run on specific day but all other days

I have a cron setup to execute a bash script daily at 10pm. I have another cron setup to run monthly on the 1st of the month. Both crons launch a bash script, and the only different in the bash script ...
1
vote
4answers
4k views

Match multiple regular expressions from a single file using awk

I'm trying to parse a HTML file using shell scripting. There are 4 different regular expressions that i need to catch: name= , age= , classs= , marks=. Using grep "name=\|age=\|class=\|marks=" ...
2
votes
4answers
3k views

FTP bulk download and rename

First of all: Why I'm trying this? Because we need to download some files AND rename to shorten ones with date "stamps". The remote files have really huge filenames and it's not an option to ...
0
votes
2answers
679 views

Unix: How to Test Ip Address is resolved to name

Unix: How to Test ip Address is resolved to name. I need to check whether ip address can be resolved to name using DNS entry via shell script. Code: DNS_Lookup() { LOOKUP=$(nslookup $1) ...
3
votes
3answers
4k views

How is it possible to sort ps command's CPU field?

I'm trying to make a small command that will find the processes that use the most CPU power. Firstly, I use ps aux > file.txt and then cut -c 16-20 file.txt | sort -n | tail -5. The result I get ...
2
votes
1answer
564 views

Execute command supplied by function parameters

I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters. Meaning somethings like this: special_execute() { # Some code ...
3
votes
5answers
268 views

Comparing Indices of two strings

I have two double quoted strings of same length, that look like this: "$con" - (consists of spaces and *'s): * ****** *** ** * ** and ...
3
votes
1answer
15k views

Concatenating two variables with an underscore [duplicate]

Alright. I need to concatenate two variables to create a filename that has an underscore. Lets call my variables $FILENAME and $EXTENSION where filename is read from a file. FILENAME=Hello ...
1
vote
2answers
114 views

Backing up a large file

I have a test running which crashes. I need to find out how far it reached before crashing. It will restart again after crashing and wipe the log file. This wiping takes 10 minutes though. So If I'm ...
1
vote
2answers
333 views

What is sourcing my shell script?

I have a .zshrc and a .zshenv in my home directory. I added a new command to my .zshenv that I discovered was being run at least 3 times. I realized that I was accidentally sourceing it from the ...
1
vote
3answers
102 views

How to transform text such that each output line starts with the block's title?

I need to make a script to transform the following: Name = x:y Z = a:b.c Z1 = a1:b1.c1 End Name=x1:y1 Z2 = a2:b2.c2 Z3 = a3:b3.c3 Z4 = a4:b4.c4 End Name = l:k s = w:e.v End into: x:y Z a:b.c ...
0
votes
2answers
430 views

sed with auto escape character [duplicate]

CommentAppend() { # Comment line and append line below commented line local comment="$1" # search this line and comment it local append="$2" # Append this line below commented ...
6
votes
5answers
7k views

Print Matching line and nth line from the matched line

I am trying to print the matched line and the 4th line from the matched line (line containing the expression I am searching for). I have been using the following code: sed -n 's/^[ \t]*//; /img ...
0
votes
1answer
251 views

How does this bash statemnet test “.$TOMCAT_USER” = . && TOMCAT_USER=tomcat work?

I am trying to understand the tomcat daemon.sh script which contains a bunch of lines along the lines test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat I figured out that test ".$TOMCAT_USER" = ...