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

learn more… | top users | synonyms (2)

1
vote
2answers
352 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
442 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
260 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" = ...
3
votes
2answers
375 views

Writing scripts that use different commands in different shells [duplicate]

How can I write a script that is shell independent? I want to be able to run one script which will run in ksh, sh and bash. For example: If I have a line in my script like whence ls which will run in ...
4
votes
2answers
727 views

Exiting out of an infinite reboot loop in startup (rc.local)

I am creating a startup script (called from rc.local) in Debian Squeeze. The startup script checks for a variable value from a file, decrements the variable and writes it back to the file, then if the ...
1
vote
1answer
217 views

How achieve variable indirection (refer to a variable whose name is stored in another variable) in tcsh

I'm trying to do the following: set a = kitten set temp_kitten = purr echo ${temp_$a} I want the echo command to return "purr". The overall idea is that I have a bunch of variables in an array and ...
4
votes
2answers
298 views

Remove all lines except D

I have scenario where my three huge files Test.txt , Test1.txt and Test2.txt has following details. H||||||||||||||||||||||| D|||||||||||||||||||||||| D||||||||||||||||||||||| H||||||||||||||||||||| ...
1
vote
1answer
273 views

How do I start smbclient from a script in Linux?

I want to be able to quickly start smbclient to a specific directory without typing or copying it to the shell command line, but just sticking it in a script doesn't seem to work... #!/bin/csh ...
1
vote
1answer
3k views

How does this su -c “…” command seem to pass two commands instead of one?

I am trying to understand the script below and I am confused about the su line. I understand the postgres command line arguments. when I do man su the manual says -c, --command=COMMAND ...
0
votes
1answer
87 views

Problem with small bash script

I often run find in my code projects when I make a change somewhere and I have to find where it impacts other pieces of code, so I thought about writing a very small script to make this easier, call ...
3
votes
2answers
5k views

Shell Script - syntax error near unexpected token `else'

With the following shell script, why I am getting errors syntax error near unexpected token `else' Shell Script echo "please enter username" read user_name echo "please enter password" read -s ...
7
votes
5answers
23k views

Add lines to the beginning and end of the huge file

I have the scenario where lines to be added on begining and end of the huge files. I have tried as shown below. for the first line: sed -i '1i\'"$FirstLine" $Filename for the last line: sed -i ...
1
vote
1answer
670 views

Error not a valid identifierd when executing shell script [duplicate]

I have the following shell script in order to connect to database and do some tasks. echo Please enter userid: read USER_NAME echo "Please enter password:" read -s PASS SID=${ORACLE_SID} if [ ...
3
votes
4answers
3k views

How do I select files in a bash script that do not have an extension?

Let's say I have files xinitrc, alphabetsoup, ieat.pie in the current directory. Need a bash script to select only xinitrc and alphabetsoup.
5
votes
1answer
123 views

Is it possible to create sticky redirection from stdout/stderr

Consider such code: echo hello >> log.txt echo world >> log.txt I execute command with stdout redirection. Is it possible to create sticky redirection? Something like this: redir ...
2
votes
6answers
7k views

Using cut/awk/sed with two different delimiters

I have the following cases: case@test.com case_1_2@test.com case_1@test.com I'm trying to convert these to case@test.com case@test.com case@test.com So it should remove everything from ...
2
votes
1answer
132 views

Redirecting an active port

Environment: Web server that requests pages on a specific port from an application server. Problem: If a page request comes into the application server during the restart of a certain web application ...
2
votes
4answers
5k views

Shell script to check for the presence of one or more files with a specific extension?

I want to write a script to find a file with specific extension.this much i have done: #!/bin/bash file="/home/Savio/Dsktop/check/*.csv" file1="/home/Savio/check/*.txt" if [[ -f "$file" && -f ...
14
votes
1answer
28k views

How can I execute local script on remote machine and include arguments?

I have written a script that runs fine when executed locally: ./sysMole -time Aug 18 18 The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script. Now, this script is ...
2
votes
3answers
5k views

Using a reference to a bash string variable in sed

I'm a relative Linux novice. Suppose that I have a text file a.txt that contains the following text: A B C Now, if I want to change the text on line 2 (which contains B), I can use the following ...
1
vote
1answer
226 views

Is there a way to see which process invoked a shell script?

Is there a possibility so see from some unix log file what process have invoked a bash/ksh shell script or is this shell script invoked manually from command line? So is there some log file which ...
4
votes
4answers
2k views

Why cat, grep and other commands can't understand files starting with minus sign? [duplicate]

If I have a file which name starting with single or several minus sign, for example --1 it can't be used as a parameter of many commands. Even if I run cat --1 instead of file content I get ...
0
votes
1answer
108 views

Converting a loop of code to function

I want to retry a command for 5 times with an interval of 20 seconds. I want this command to be passed as a method parameter. How to do it? And once the function is written how to pass the value to ...
4
votes
7answers
2k views

how to share environment variables between shells like globals related to a master pid?

So, I need to share environment variables between shells, and that the changes on the variables be promptly recognized by all shells, like global variables. But they must be related to a master pid, ...
1
vote
1answer
583 views

Automation tasks for system administrators [closed]

If I am a junior UNIX/Linux system administrator, and I want to make automation tasks for the server. I know Shell script, Perl, and Python. All of these are used to make automation tasks. Which one ...
3
votes
1answer
2k views

Start 4 programs on startup in Linux Mint via terminal

I'm working on a project and I hope you can help me solve a problem. I have four programs that are written in C. They have been compiled using gcc and they work as they should. Because the four ...
4
votes
2answers
2k views

Optimizing a shell script with long running while loop

I have written a shell script which has to do the following: Capture session commands into one file. Every individual command into separate file. Mail every individual command file content based on ...
2
votes
3answers
3k views

$PWD variable equivalent of pwd -P

I want to use the $PWD variable in a script, but I want it to be the hardware path without symlinks. I know about /bin/pwd and pwd -P, but those aren't variables. I know that I can use: setenv MYPWD ...
2
votes
3answers
731 views

How to grep the last number on a line in a bash script

I have output from the smartctl -a command; one of the lines looks like this: 9 Power_On_Hours 0x0032 100 100 000 Old_age Always - 301 I've placed this into a variable (if that's the correct term) ...
3
votes
3answers
2k views

Concatenation of two fields of two files

I have two files : called t1.txt and t2.txt. The contents are : t1.txt a b c t2.txt ab bc cd Now I want to merge these two files line by line into t3.txt so that the t3.txt should look like : ...
4
votes
5answers
864 views

Is there a way to listen to process?

I want to know if there is a way to listen to a process in linux & unix - when it ends and what's the exit code of the process(script). I dont want to write a script that will run X seconds and ...
4
votes
3answers
914 views

Quote a variable in shell script “if” statement

e.g. if [ "$FOO" = "true" ]; then vs if [ $FOO = "true" ]; then What is the different? Seems both of two statements also works.
0
votes
1answer
61 views

Get the most recent backup files [duplicate]

I'm googling for this, but the terms are so generic I'm getting way too many false positives, so what the hey, I'll ask here. I'm sure there's a simple answer but I don't generally script in *nix. ...
0
votes
1answer
208 views

Removing lines based on tags for different files and merging into single file

I have to develope unix script in such way it should meet below scenario. I have 3 text files, 1.txt , 2.txt and 3.txt Data in those files as below. HDR|1|2|3||||||||||||||||||||||| ...
2
votes
3answers
4k views

Executing a program with various parameters (loop)

I have a program, prog, which takes an integer argument when I call it from the terminal, eg: ./prog 10 I would like to execute prog with a range of values, e.g. 10, 20, 30, .... Is there a neat ...
4
votes
3answers
113 views

How to condense subdirectories

I have one directory with many sub-directories. All of those sub-directories contain files which each have unique names. I want to take all the files from all of the sub-directories and move them all ...
1
vote
2answers
215 views

Remove complete line in text files based on Tags

For example : 01.txt, 02.txt, 03.txt and 04.txt has following values. HDR|A|B|C|D|F||||||| DTL|1|2|3|4|5||||||||||| DTL|5|6|7|8|9||||||||||| TLR|A|B|C|D|F||||||| I have to remove complete lines ...
1
vote
2answers
270 views

How to extend a command in bash so that it expands based on options?

I've started working with a git repository that includes a large amount of content that should be assumed readonly. So, every time I throw a new branch: git checkout -b newBranch it should be ...
2
votes
1answer
1k views

Why does this Bash NRPE plugin not return a variable to Nagios?

This is a very specific question I have here, maybe one has a simple answer for it. This script I have here works locally just fine: #! /bin/bash volts=`sudo vcgencmd measure_volts core|sed ...
1
vote
1answer
2k views

How to integrate a multiline awk script in a shell script

I am a beginner in shell scripting and my question is a continuation of How to parse a file to extract 3 digits numbers kept in a "group number" I am trying to integrate in a single shell ...
3
votes
2answers
152 views

bash equivalent of zsh's $@[2,$#]

(NB: the motivation for this question is only to improve my bash programming know-how.) I want to know the bash equivalent of zsh expressions such as $@[2,$#], which directly address a range of a ...
0
votes
1answer
591 views

What is the purpose of xargs -I command?

I'm Unix noob, can you explain what this command does: files=(this_is_filename) for filename in ${files[@]}; do ls -t1 ../htory/$filename* | head -1 | xargs -I fname cp -p fname ...
2
votes
1answer
73 views

Pipeline each line of a file to a specific variable in a second command

I want to execute the following instruction: basex -bword=ENTRY consulta.xq But for every line of a SampleText file (plain text mainly). The SampleText contains the following lines: hello evening ...
1
vote
3answers
105 views

Confused on user and su in linux

How could I run a script as a specific user (non-root)? As this? sudo su - john /usr/share/script.pl for user john?
0
votes
1answer
1k views

if condition, loop in grep [closed]

Could someone please provide help for me. I tried to start writing a shell script but am getting hung up with this issue. I need to grep commands "adb shell ps" and find in it 3 processes with names: ...
3
votes
1answer
290 views

A robust way to execute a command/run a script at login?

I have two commands that change keyboard behaviour: xset r rate 120 66 /usr/bin/setxkbmap -option "ctrl:nocaps" When I open a terminal and execute them, they change auto-repeat settings and remap ...
3
votes
4answers
369 views

Show video files with its size

I need all the .mp4 files' dimensions to be printed with the file name. For example: 1_VIDEO.mp4 1204x680
1
vote
1answer
367 views

What is the pipe character “x|y)” in the case statement?

In a script /etc/network/if-up.d/avahi-autoipd in my system, I find statements like this: case "$ADDRFAM" in inet|NetworkManager) ;; *) exit 0 esac case "$METHOD" in static|dhcp|NetworkManager) ...