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

learn more… | top users | synonyms (2)

1
vote
3answers
71 views

Extract substring with character instead of position

I recently got into nautilus scripts, and for the one I'm writing I'd need to extract a substrong from a filename. My problem is that I found tons of methods to extract a substring based on the ...
1
vote
2answers
37 views

Prefix and suffix strings to each output line from command

I have ran into a problem trying to write a Bash script. When grep outputs, it returns (usually) many lines. I would like to prefix and suffix a string to each of these output lines. I would also ...
1
vote
5answers
144 views

Add quotation marks & comma to each word in a file

I have a list of words such as: string1 string2 string3 .... string12312 How do I convert them these words in such manner that output could be used as a JS array, i.e. "String1", "String2"..., ...
4
votes
2answers
52 views

How do I find the PID of a particular program?

I would like to write a Bash script that searches for PIDs matching a program's name (perhaps using ps ax | grep <PROGRAM> or something similar) and feeds them to pixelb's ps_mem script. ps_mem ...
-1
votes
4answers
71 views

bash script to reverse line

So i came across this script to reverse an input line #! /bin/bash input="${1}" reverse="" len=${#input} for(( i=${len}-1; i>=0; i-- )) do reverse="$reverse${input:${i}:1}" done ...
0
votes
1answer
43 views

Bash script not working, doesn't change directory

First off, I only just started learning how to use Linux yesterday so my abilities are very limited. I am essentially creating a script that I can run everytime I turn on my VPS and the script then ...
0
votes
2answers
48 views

Find value in one column and print corresponding value from other one

I have text file with values as: 1 Peter 2 Paul I need a command/scipt that will take as a parameter value from the second column (e.g. Paul) and return the corresponding value from the first one ...
4
votes
2answers
46 views

Redirecting bash loop output to variable specific files

this should probably be obvious to me, but I've been stuck for some time. I'm trying to write a very simple bash loop to take a list of servers, retrieve some specific info from them, and save the ...
15
votes
6answers
561 views

Should variables be quoted when executed?

The general rule in shell scripting is that variables should always be quoted unless there is a compelling reason not to. For more details than you probably want to know, have a look at this great ...
-1
votes
1answer
37 views

Calling a function and storing the value in a variable

pid=`ps -ef | grep wcm2_wcm2appwlx | awk '$0!~/grep/ && $2~/[0-9]/{print $2}'` for I in 1 2 3 4 5 do sts=$(check) if [$sts eq / true/] then echo $I slp else break ...
3
votes
2answers
81 views

Passing paths with spaces as arguments

I am having difficulty in passing some string variables having spaces in them as arguments to a program. For debugging and showing the arguments being passed, I created a demo Python script -: ...
1
vote
2answers
37 views

Rsync deletes files in old directory?

I'm really stuck on an rsync problem and I haven't been able to crack it by reading the documentation. I want to make 7 directories, one for each day of the week, for back-ups of some folders and ...
13
votes
2answers
716 views

Why does “bash -x” break this script?

I have a script which measures how long some command executes. It needs the "real" time command, meaning, a binary for example in /usr/bin/time (as the bash-built-in doesn't have the -fflag). Below, ...
0
votes
0answers
26 views

Shell ( := :- characters ) [duplicate]

I just started learnning shell script. The following snippet of code makes me wonder what does the symbols := and :- mean in line 4. while read perms links owner group size month day time file do ...
1
vote
2answers
66 views

Select filename with highest version number

highestVersion() { for TEST_PATH in `ls -a "$/TEMP"/*.flg` do echo " you are here" TEST_FILE=`basename "$TEST_PATH"` echo "$TEST_FILE" done } Below is the file ...
5
votes
7answers
294 views

Remove lines from tab-delimited file with missing values

I have a large (~900MB) tab-delimited text file that I will be processing in a downstream program. I need to delete any line with a missing value. The correct number of columns are on each line (so ...
-1
votes
1answer
63 views

bash - sed: -e expression #1, char 15: unterminated `s' command

I try to use sed command inside a loop to replace IP address, but it is always giving me this error: sed: -e expression #1, char 15: unterminateds' command` even it is not missing the end slash. ...
0
votes
1answer
30 views

How do I write a Bash script that will ask users to define a variable?

I am presently modifying this shell script to turn it into a shell script for installing Arch Linux, with custom username(s), password(s), etc. At the moment I am wondering what I need to do in order ...
6
votes
5answers
511 views

Replace string after last dot in bash

I have the following pattern in a string (an IP address): 123.444.888.235 I want to replace the last number after the dot with 0, so it becomes: 123.444.888.0 How could I do it in bash or ...
0
votes
1answer
33 views

Tar files in order

I am taring files using this script tar -cvf test.tar -C /home/user/Desktop/filestoTar File_one zap_file tar -rvf test.tar -C /home/user/Downloads/test Auto_file rocket_file And I want to untar ...
-2
votes
3answers
46 views

Modify all bash commands through a program before executing them

I am trying to create a program which require such feature. The flow will be like: User enter a bash command User hit the enter My script will get command, current directory,.. as variables. Program ...
2
votes
0answers
36 views

How do I write a Bash script to autoadjust the screen resolution of Enlightenment 20 running under VirtualBox?

I have noticed that VirtualBox guest additions fails to autoadjust the screen resolution of Enlightenment 20 on a few different platforms (including Arch Linux, Lubuntu and openSUSE Tumbleweed), see ...
10
votes
2answers
709 views

Is there a POSIX way of setting zeroth argument of a target application?

In bash you can use exec -a and in zsh you can alterntavely set ARGV0 to execute a program with a certain zeroth argument but is there also a POSIX way of doing so? As suggested in this one comment ...
0
votes
3answers
42 views

bash - Change line by line Interactively

I am new in bash scripting. I want to create an interactive script which is prompt user to enter data for editing a file line by line. The scenario: - Read a file and iterate over each line, I use ...
1
vote
2answers
52 views

How to fetch file with highest version number

abc_efg_2015_hanshake_01.csv.gz abc_efg_2015_hanshake_02.csv.gz abc_efg_2015_hanshake_03.csv.gz how to fetch file with highest version number and store it in a array.
2
votes
1answer
53 views

how to force shell script characters encoding from within the script

I have a few shell scripts with UTF8-encoded characters inside and i want to be sure that they are decoded correctly regardless of the machine locale settings. Is it possible to force the shell (bash ...
2
votes
1answer
61 views

Shell Script Loop Conditionally

I just create a new shell script and having trouble with small issue. Below is my script :- function mainMenu(){ while : do echo -e "\nMain Menu:" echo " A. Process Managements ...
1
vote
1answer
36 views

globbing and $# [closed]

Suppose my shell script gets 3 arguments: ./myscript dir1 material dir2 I need $# to report the count of items globbed with dir1/* and not the count of my script's original 3 arguments. Do you ...
-3
votes
1answer
38 views

shell script to sleep at an increment of 10 [closed]

shell script to sleep at an increment of 10 lines. I want my script to sleep for 3 mins after every 10 lines its read. then pick up from the last line. I'm not good with script, so I dunno where ...
1
vote
4answers
59 views

How to get only one character after match in string (to determine RHEL version from rpm)

I have an open source shell/bash script for setting up and configuring hosting servers, and to install cloudflare I am pulling the version of RHEL using this command: RHEL_VERSION=$(rpm -q --qf ...
0
votes
1answer
35 views

Why space is being counted as newline in shell script? [duplicate]

I wrote the following script. I want to read a file and print as line by line but it considering space as new line. why is that? #!/bin/bash for i in `cat data.txt` do echo $i done Input: Saved in ...
0
votes
3answers
35 views

How to add two variables passed in the shell which returns integer count?

In my script I am using 2 variables: integer CCD_COUNT1= `grep '.CCD' $TMP_RPT_FILE1 | wc-l` It returns integer value suppose 47. Another variable: integer CCD_COUNT=`grep '.CCD' $TMP_RPT_FILE | ...
2
votes
2answers
75 views

Passing multiple of arguments with whitespaces through a script to ssh

I want to remove multiple files from remote server. I have all files under one array in a script and I call other script which will remove files. Let output of "${b[@]}" is: ...
3
votes
2answers
130 views

How do I remove all but the file name (with no extension) from a full file path? [duplicate]

I am presently writing a Bash function to convert all the man pages listed by equery files <PACKAGE> | grep /usr/share/man/man (if you are unfamiliar equery is a tool used on Gentoo-based ...
1
vote
3answers
36 views

script not running as expected when scheduled as a cronjob

I'm trying to write a script that will identify corrupt jpg images using imagemagicks identify command. The script will run the command, grep the output for the word "Corrupt", move it to a corrupt ...
1
vote
2answers
43 views

Add Line of Text to Section of Rules File

I'm trying to write a bash script to automate the install of nginx with pagespeed module. Part of this requires me to add this: --add-module=$(MODULESDIR)/ngx_pagespeed \ to a section of the ...
2
votes
2answers
43 views

Check Input Prior to Executing a “for loop”

is there any way to check or test the input from a 'read' against a for 'loop'? I don't want the case statement to execute there any of the input is incorrect. As of right now it will loop through, ...
5
votes
3answers
646 views

File name beginning with - (dash) [duplicate]

I have file called -l in my directory now I tried to do for i in *; do stat -c "%s %n" "$i"; done it lists all files with sizes but in the middle of the output there is something like 395 ...
1
vote
1answer
61 views

wait for predefined time before exit the script

I have an app server running in my unix box with process name as abc_test. I have a shell script in which I have to stop my server using /opt/abc/bin/stop. Now sometimes my server doesn't get stopped ...
0
votes
3answers
36 views

Matching lines in two files [duplicate]

Let's say I have two files, file1 and file2. file1: passd:xxx hopla:alli gnar:gungg araf:utap file2: alli utap How can i check what lines/words from file2 match file? Indeed I could probably do ...
2
votes
2answers
209 views

Solaris using sequence generator at the beginning of each new line

I am new to shell scripting. I created a script that extracts data from a database and loads it to a csv file. I am using SunOS hstz1454 5.10 system. I am trying to do a sequence at the beginning of ...
5
votes
3answers
105 views

Auto increment filename

I have a need to collect some duplicate files and want to avoid name collisions. The problem is that this collection of files might be added to by another execution of my script before the files are ...
1
vote
1answer
28 views

handle large number of same log using cron or logrotate

We have an app deployed in a server, which, under some scenarios, generates some logs in the below format. process_name.hostname.common_text.log.{error/info/warning}.date.time Now, due to this ...
3
votes
4answers
412 views

Sum of all Integers between two numbers

I would like to type in two numbers and get the sum of all integers between those two numbers, inclusive. For example: ./sum1 2 5 should yield sum: 14 because 2+3+4+5 = 14 Here's what I have ...
-1
votes
3answers
82 views

bash script to login to multiple Linux servers and collect the output of multiple commands through ssh [duplicate]

I need to login and collect output of below commands from multiple GNU/Linux servers through SSH. I have done password-less SSH authentication. I wanted to do this with bash script. df -lh | awk '{if ...
3
votes
3answers
159 views

I need to take the output of command and make it the name of a file using a script [duplicate]

I am working on a way to integrate our Linux machines into our SCCM Reports. One of the things we would like to do is verify the swap size on the machines. I know that I can get the swap amount on ...
3
votes
5answers
71 views

How to process special characters in in file name using find

I am to find all files that start with certail char , e.g find . -maxdepth 1 \( -name "^m*" -a ! -name "g$" \) -print but what if someone create file that has special characters in the name of that ...
1
vote
2answers
148 views

cat command is not working inside shell script

I have the following line which is not working in ksh but works fine in sh cat `find /home/peter/databases -name "cells.txt"` This is working if this line is inside /usr/bin/sh script. But not when ...
2
votes
2answers
49 views

how to login using ssh with password? [duplicate]

I want to login using ssh and password store in a text file or may be in a varible I am trying below method but i am getting an error please suggest me correct way. echo "123456" | ssh ...
2
votes
1answer
15 views

Script to see /mysqlshare is present in all list servers, doing only first iteration

I need to get /mysqlsharedetails from all the MySQL servers. I have written a script using a while read loop, but it's getting the details of only the first server, #!/bin/ksh ...