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

learn more… | top users | synonyms (2)

0
votes
1answer
40 views

Bash to clear historic content folders

I require a bash script to keep the most recent 'date_YYYY-MM-DD_HHMMSS' directory, removing others The directory structure is as follows. The directories in tags are variable and will change, i.e ...
2
votes
1answer
95 views

Iterate over bash array

I want to use the values of one array to point to values in another, but I can't figure it out. I think this is very basic. I'd like the file to yield one line associated with each of x, y, z as ...
2
votes
2answers
47 views

How to stop bash scripts from exit ing the window altogether

When I write bash script and I have exit;; or exit 0;; the script not only exits but the window (or pane in the case of tmux panes) exits completely (goes away). e.g. while true; do read -p ...
2
votes
2answers
46 views

Bash script to insert information after each line of block of data

I wish to write a script that would insert information from the /proc file system to a list of processes generated by top. It would be something like this: Here is the sample output that I get from ...
0
votes
1answer
36 views

Idiomatic bash way to run optional action without side effects on exit code

This script will exit with test result which is not desired from callers perspective. How it should be implemented instead? #!/bin/bash test -n "" && echo "test passed"
-1
votes
1answer
22 views

How does the shell script variable, that holds a command, work? [closed]

I'm trying to create an auto-compiling script that will compile programs from source, whenever I boot up my computer. It currently goes roughly like this (I can't remember the exact syntax): ...
1
vote
0answers
39 views

Print Dialogue configuration to command line

I need help figuring out which program gets called with what all arguments when I print a file using the standard print dialogue with a specific configuration, so I can automate it using a script. ...
1
vote
1answer
16 views

Why won't this example of setUID work? [duplicate]

I am learning about setuid and setgid where the user executing the script will inherit the owner's permission while running the script. To test this, I created a bash as user fiverr in ...
1
vote
0answers
28 views

/bin/csh Event not found ? C-shell

I know this question has and does come up a lot (I know because I pounded Google to death and often came back here from the redirects). Below is my script. I'm not entirely certain what I am missing, ...
1
vote
3answers
35 views

How to extract lines starting with a certain letter?

I have a texfile with a many lines in it, Example lines: AABBA, 10050, Tom marc Peter , POR BBBAC, 10051, Eva braun, AUS CCAHA, 10052, Jon murry , UDD GGHAN, 10053, Taler yon, AGG IIUSA, 10054, Raff ...
1
vote
1answer
26 views

clear command in console dialog

My bash script contains the clear command, that when executed through dialog command will result with strange letters. How can I still use clear command without seeing this output in the dialog? This ...
1
vote
1answer
23 views

Exiting a running script with any button

I have a script in which I'm trying to add the functionality of exiting upon keypress. Thus far, my code looks like what you see below: keyinput='' if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 ...
0
votes
2answers
54 views

for loop in bash

I want to create a for loop that runs from 1 to a number stored in a variable. But it produces this error: Syntax error: Bad for loop variable This is my script: #!/bin/bash count_lines () { ...
1
vote
2answers
38 views

Create symbolic link recursively, if file 1.JPG exists rename it to 1A.JPG

I have a shell script for creating symbolic links of all my photos from one year. The photos are from different cameras and are in subdirectories. f=$(pwd); export f; q="2015/"; z="2015_Links/"; find ...
-1
votes
2answers
39 views

Nested echo command in backticks

I want to know why an extra echo will print in my shell. I'm using bash version 4.2.46(1). echo `echo `echo $SHELL`` A interesting issue is if I replace '``' with $() it doesn't print extra echo: ...
15
votes
3answers
821 views

How to get HOME, given USER?

I have an USER variable in my script, and I want to see his HOME path based on the USER variable. How can I do that?
1
vote
1answer
43 views

Record cpu% reading in period

My question is let the user determine the period of recording down the CPU% reading and my code is like this #!/bin/bash watch -n $1 ps -eo pcpu it's correct or not?$1 is an argument.
2
votes
2answers
41 views

How to check if directory is mounted on disc X and mount it if it isn't

I'm trying to write script to back my data up. I don't know much about it, so I might take a wrong approach, but what I'm trying to do is use rsync -avh several times for each directory I want to back ...
1
vote
1answer
42 views

Trying to Use awk by implementating a condition and implication

I have a file containing 8 columns, One of my tasks is to get the values of Column 3 and if the value of the row is not equal to 123 the 8 coloumns will be printed and hence If column 3 is equal to ...
9
votes
5answers
361 views

Find last occurence of string in multiple files

I need to search multiple log files (all files generated in last 24 hours, all kept in same directory) to find the last occurrence of a string. This is the command I wrote: find . -mtime 1 | grep ...
-1
votes
1answer
133 views

syntax error near unexpected token `fi'

I'm getting a syntax error unexpected token fi and Permission denied in the following script: cd /home/NorthStar/Dhruva/server-specific-scripts/crons/ResetETA su postgres -c "psql -d "Dhruva" -f ...
1
vote
1answer
76 views

Shell script stops automatically after around 1 minute

I wrote this very ugly shell script that, at one point, worked perfectly. Now it works for a minute or two and then exits. I know its very ugly but it worked so I never changed it. Could anyone tell ...
0
votes
1answer
130 views

Make a shell script execute commands in telnet or programs

I would like to make a shell script that executes commands on a device that I telnet to or in programs such as FTP or OpenSSL. I have already found a method in FTP, which would look something like ...
1
vote
2answers
79 views

Unix shell script, parameters with spaces

So this is homework help for my intro to unix course. I need to make a shell script called chExt.sh. that accepts a file extension and then multiple files. It then needs to change the file ...
0
votes
2answers
48 views

Conditional grepping

I have a scenario to handle. Need help with that... I have a config file and its content looks something like below: Jobname|Type|Silo|Description #comment1 #comment2 ...
0
votes
1answer
46 views

Sort multiple csv files based on two columns within a directory

I have multiple .csv files in a directory called mydirectory. I want to sort all these files using some bash/awk/sed command first based on LeftChr column and then RightChr column and get the result. ...
1
vote
1answer
42 views

Convert Key:Value paired multiple data blocks into csv file

I have multiple Key:Value pair data blocks separated by delimiter "*************" in an text file.The help required here is for a shell script to convert them into csv file. ************* Name : ...
12
votes
7answers
524 views

bash if not multiple conditions without subshell?

I want to combine multiple conditions in a shell if statement, and negate the combination. I have the following working code for a simple combination of conditions: if [ -f file1 ] && [ -f ...
1
vote
2answers
42 views

can anybody suggest me how to write script on the below problem? [closed]

Can anyone suggest how I can use this step in the script (that I need to take the output of the first command i need to use it second command for all three)? [aaa@build]$ find . -name src ...
2
votes
1answer
33 views

How to emulate returning arbitrary values from shell functions?

Shell functions are just statements and they don't return values. Can anyone share best practice on writing functions that return values in bash? Let's say I've a function that joins two strings: ...
1
vote
1answer
67 views

How to recursively remove all but list of files?

How can I recursively remove everything in a directory EXCEPT a small list of files to preserve? For example, let's try to remove all files except ones named f2 and f5. # Create a testing ground ...
2
votes
1answer
38 views

How to know log the full script name executed by user?

I want to log the full name of the script executed by the user including the options/arguments he provided to that script. I was able to achieve some success using basename and whoami but it does not ...
1
vote
2answers
28 views

How to control a backgrounded subshell?

If I run the following command at a bash prompt: (for i in {1..100}; do echo $i; sleep 1; done) & It will start counting and immediately return me to my prompt. I can continue working normally, ...
3
votes
3answers
167 views

Repeating command with different filenames

Here's a layup for someone... I'm having to run a command repeatedly: $ wp input csv MyCSV01.csv directory_name $ wp input csv MyCSV02.csv directory_name $ wp input csv MyCSV03.csv directory_name ...
1
vote
5answers
108 views

How can I get the number of spaces characters in a string?

How can I check how many white spaces (' ', \t) there are in the first line of a file? Thanks
-3
votes
1answer
26 views

Help required in logic

I have a below requirement. I am validating one of my code-file using one utility in shell script and writing that Error,Warning messages to an output file. Now i need to write a logic like if i ...
1
vote
1answer
30 views

how do i make a script to generate a gpg backup saver?

Im making a little backup script that includes the function to encrypt the backup. The script runs automaticly with a cronjob. So the password for gpg is in a File. Its just a textfile. How can i ...
3
votes
3answers
98 views

add absolute path option to a command

Given a shell command (let us call it "oldcommand"), is there a simple shell script newcommand.sh which will behave as oldcommand except that it will have a new "-location" option, and for example, ...
6
votes
3answers
222 views

Exit status in bash script

I am trying to create script which can let me to put number of port as parameter and then find the name of service for this port. Here is the script: #!/bin/bash grep -E "\W$1\/" /etc/services | ...
-2
votes
1answer
24 views

The script, which will take IP from the file etc/hosts [duplicate]

I write script, but this does not work. I need the script, which will take IP from the file etc/hosts. How to do it? Script: (This does not work) host=www.example.com ip=$(getent hosts "$host" | ...
0
votes
2answers
33 views

How to use a special shell variable in If then statements

I'm fairly new to scripting and I want to learn how to check the values of $? to check whether the a grep command was successful, If the grep command is not successful, then to echo a message which ...
-1
votes
2answers
75 views

Resolve domain script [closed]

How to make it work? Script 1: (This does not work) host=www.example.com ip=$(getent hosts | grep $host | awk '{ print $1}') echo $ip Script 2: (This does not work) host=www.example.com ...
1
vote
1answer
145 views

Substring extraction with expr

I was working on extraction of sub-string from the beginning & at the end of the string. I experimented with and with out ".*" $expr "ab1cd1efgfedcbaAAAA" : ".*\(1[a-l]*\)" # Result: 1efgfedcba ...
0
votes
1answer
24 views

How to recognize the number of inputs in a file and display the appropriate message?

Lets say I have a file called large. In the file is just a bunch of cal commands copied for about 100 lines. Is there a way to run something at the end of this file that will show the total number of ...
0
votes
2answers
44 views

How to unmount an usb stick from a script which is on the usb stick?

How I can I unmount a usbstick right from the script which does kill itself. Because i'm running the script from the stick it's not possible to just unmount it because theres always the error: device ...
1
vote
3answers
56 views

Complete key value pairs

Background: I want to write a bash script (MacOS 10.9.5) that inserts values associated with some strings. In the script, I will define each of the possible associated values. For example, I can ...
0
votes
1answer
22 views

Assigning a command to an array in shell script isn't working?

When I run run the command in the terminal it assigns the array. $ FILES=($(ls ~/Desktop/TEST/)) $ echo ${FILES[@]} file1.txt file2.txt file3.txt But as soon as I close the terminal and run it in a ...
-1
votes
1answer
58 views

Comparing and remove files

In linux, i have one file which having only file names inside( 60 file names only,no other contents), file60.txt foo foo123 ... bar and another file with 63 file names, mostly the same, ...
0
votes
1answer
25 views

Can't find logical fault in script which should execute a tcpdump

I have written a script which compares the time and if it is the same start time like the current time and not like the end time it starts a tcpdump. And If the end time is like the current time it ...
3
votes
2answers
257 views

Looping through a shell array given as parameter

I'm trying to write up a little helper script that will change permissions and ownership to some sites on a server. Right now, I can either pass in 1 site, or do all via simply skipping that ...