Questions about bash only, as opposed to other Bourne/POSIX shells. For all Bourne/POSIX-style shells, use the /shell tag instead.

learn more… | top users | synonyms

4
votes
2answers
193 views

Best and short way to run a program if another is not installed

I have this code: #!/bin/bash num=${1:-undefined} cmd=$(which {banner,echo} | head -n1) until [[ "$num" =~ ^[0-9]+$ ]]; do read -p "Type a number: " num done for ((num;num>=0;num--)); do ...
2
votes
2answers
56 views

cp won't work on current bash script's directory

I'm trying to copy some files from current directory in a bash script but the problem is that the cp command doesn't work with the current directory. I can use the following command without any ...
3
votes
3answers
163 views

Environment variables. Where are they saved in? [duplicate]

If I do not edit the .bashrc or other config files, the environment variables that I've setted are gone when I logout, or turn off the terminal. What I'm curious is, where are those 'temporary' env ...
0
votes
1answer
43 views

Color diff output

I am new to unix I wanted to format the unix files conditionally , I am currently working on diff command and wanted to know if it is possible to format the text of the diff command output. Example: ...
0
votes
1answer
48 views

How can I get a shell script to change directory for me? [duplicate]

here is a small test I ran, to try and move me back to root: #!/bin/bash pwd cd / pwd It returns the current working directory, and then the correct new one, but when the script finished executing, ...
0
votes
1answer
29 views

Hide curl output [duplicate]

I'm making a curl request where it displays an html output in the console like this <b>Warning</b>: Cannot modify header information - headers already sent by (output started at ...
-1
votes
0answers
36 views

Bash read user input and then call for a curl [duplicate]

I'm trying to execute a bash script that accepts user input and then according to that user input executes a curl request, the user input includes the query string that should be called using curl. ...
-2
votes
0answers
32 views

Unix KSH/Bash : Diff files for selected fields/columnwise only {GNU Bash 4.2 Environment}

I have two files: F1: abc|abc|abc|abc|abc| abc|abc|abc|abc|abc| abc|abc|abc|abc|abc| F2 abc|abc|abc|abc|abc| bcd|bcd|bcd|bcd|bcd| My questions are as follows: Suppose both files will have same ...
4
votes
4answers
294 views

How do I extract parts of a string to variables?

I have a line from some file like this: attempting to create a 512^3 level (with Dirichlet BC) using a 16^3 grid of 32^3 boxes and 800 tasks... I want to extract the 512^3, 16^3, 32^3 and 800 four ...
0
votes
1answer
26 views

Sanitise bash arguments

I have a script which takes user input and does a lot of things. One thing which I am worried about is it takes a file name and removes that file. e.g. rm -rf $arg1. A user could technically input / ...
1
vote
4answers
39 views

Anyway to pass a variable to bc, having a command to be executed?

I need to pass certain variable to bc to get the output in floating point, var1=$((<some operation>)) var2=$((<some operation>)) #Needs var1 var3=$((<some operation>)) ...
-5
votes
2answers
61 views

How to average a column in a text file without using ' character? [closed]

I need to do it without using the ' character. I could do it like this: awk '{sum+=$5} END { print "Average = ",sum/NR}' except this contains a '.
3
votes
1answer
40 views

Is it good practice to make an init script's lockfile immutable?

I recently created an init script, and decided to set lockfile to immutable to prevent external interference. The only downside I can think of is that if someone wanted to recursively delete ...
1
vote
2answers
78 views

One-Liner needed: Stop httpd if running already & Start httpd if not running

As part of a provisioning script for CentOS 7 I am in need to have a one-liner that performs the following. Unfortunately, I have no clue how to achieve that. If httpd is running then stop it If ...
0
votes
1answer
74 views

How to get rid of “\r” a the end of the line comming from pipeline in bash

#!/bin/bash sourcenode=$1 destnode=$2 path1=/root/test ssh root@"${sourcenode}" -t "echo "${path1}" | xargs -IDIR find DIR " | while read line do ssh root@"${sourcenode}" -t "stat -c "%a" ...
3
votes
2answers
28 views

locally created alias not being used if called using backticks (`)

I have come across this issue while trying to build up a script to do grep on multiple files for 10 different word. The script I created, is (minified) #!bin/bash alias grep='grep -n' out=`grep word ...
0
votes
0answers
27 views

Edit Oracle Path on Ubuntu Virtual Machine

I have two virtual machines ubuntu 14.04 LTS, one is a server when I installed oracle 11g express, and the other is a simple client. My goal is to run an sql query remotely from the client machine. So ...
1
vote
1answer
37 views

Compare n no of column values between each record in 2 files and apend data from file 2 to file 1

I am new to unix so wanted to understand how this(mentioned below) can be done. I have an input file like this: Example for input file: a|a1|a2|a3|a4|a5|...|a29|$2342 a|a1|a2|a3|a4|a5|...|a29|$2343 ...
0
votes
0answers
52 views

De-dup the file based on combination of fields and store the last value for duplicates

Scenario is like: I have a sorted input file(pipe delimited) which has dupes (except last column value) in it. Now i need to check each of my input lines and decide if it's duplicate. If yes then ...
1
vote
2answers
27 views

How to merge the lines in Unix (Sun Solaris) in a specific pattern

I have below n number of lines in a flat file: uk,1234560000 uk,6789067000 uk,4567890000 . . . n lines Now I have to make it in below format: ...
1
vote
1answer
75 views

Using the grep filter

I have a text file with the name "hello.txt" that contains the following two lines: Hello WORLD! Hallo world! When I wrote the following grep -i h[ae]llo hello.txt in the terminal, the output ...
0
votes
1answer
22 views

Nnumber of HTML redirects from cURL

My little script extracts DOI's from a csv files in order to check whether they are right. The number should equal 3, and if it doesn't, I want to output an message. #!/bin/bash ...
0
votes
1answer
22 views

How to redirect output of a 'xterm -hold -e' command

I know you can use tee to log output from a command in a terminal, e.g.: df -h | tee ~/log.txt However, how can I do this with a more complex command like this: xterm -hold -e program arg1 arg2 ...
0
votes
1answer
16 views

trap to debug signal was evoked twice before shell function executed when 'functrace' is on

My aim is to do something before every user-defined function executed. The most straightforward method coming to my head is to use trap to debug signal. The pilot script run quite well: #! /bin/env ...
0
votes
1answer
49 views

Hyphens in variable values (in bash) [closed]

I'm writing a simple script for iptables to restrict access using MAC addresses. First, I check if an address has been passed to the script: ALLOWED_MAC=$5 MAC_STRING="" if [ -n "$ALLOWED_MAC" ] ; ...
2
votes
5answers
394 views

Problems when trying to execute sh file from another sh file

I have the following script: #!/bin/bash if test -f "/path/pycharm.sh"; then sh ./pycharm.sh; fi I am trying to run pycharm.sh from a bash file and I've looked carefully to give all the ...
0
votes
2answers
51 views

Writing a Script to match the architecture of system and software

I am trying to write a script where it will cross check to things: 1.The architecture for which the setup file was intended (32 or 64 bit) 2.The Architecture of the system. The second part is quite ...
0
votes
1answer
43 views

shell script unable to set environment variable with the grepped value

In the shell script, I want to grep a value from the log file and set as the value of one environment. In the log file, there is one line like this: SOME_PATH=/some/specific/path In my shell ...
2
votes
1answer
46 views

Using fixed strings in regular expressions?

I am writing a bash script which accepts a list of CSV files as arguments and outputs e-mail addresses only found in the first file. To accomplish this, for each record in the first CSV file I look ...
1
vote
1answer
49 views

Cut some fields from a text file to another

I have the following command: grep -owf champs.txt t.txt and I want to cut the result of this command from the file t.txt to another file. I know that it can be achieved by a nested command but I ...
2
votes
2answers
99 views

Passing a bash command line arguments containing #

I am trying to pass pound sign as a command line argument then echoing the argument. When I pass for example 2 # 2 then I echo $1 $2 $3, I assume the terminal will output 2 # 2 but it outputs just 2. ...
3
votes
3answers
115 views

Bash script command line argument to upper case

If I can do this in my bash shell: $ STRING="A String" $ echo ${STRING^^} A STRING How can I change my command line argument to upper case? I tried: GUARD=${1^^} This line produces Bad ...
0
votes
2answers
36 views

Auto Delete Inactive files after x

Is there a way I could delete certain folders after x amount of days of inactivity? I was thinking of using cron but that wouldn't take the inactivity part into consideration. Is there any Debian ...
0
votes
2answers
69 views

bash execute command for X arguments at a time

I have a list of files stored in txt file. ./test7 ./test4 ./test1 ./test5 ./test6 ./test10 ./test8 ./test2 ./test9 ./test3 I want to run a command on all those files but I want to sleep 1 second ...
1
vote
2answers
83 views

check if a variable is in list

to check if a word is in a list of words or no I wrote this script: tr -s '[[:blank:]]' '\n' < t.txt | while IFS= read -r word; do if [[ "$word" =~ $(echo ^\($(paste -sd'|' ./champs.txt)\)$) ...
1
vote
2answers
43 views

I cannot understand bash script results

Here is the script: echo '1 2 3 4 5 6' | while read a b c ;do echo result: $c $b $a; done The result is 3 4 5 6 2 1 Can someone explain why?
0
votes
1answer
33 views

Error not found [closed]

I wrote this script to encode / encrypt data from a sql query by changing the names of tables and fields (I played on the ASCII character codes). #! /bin/bash chr() { [ "$1" -lt 256 ] || return 1 ...
2
votes
1answer
30 views

Bash completion - How to get filename completion for suboptions

I have a function which can take main arguments and then sub-arguments. Some of these sub-arguments are filenames. The current completion code I have is as follows. _scm() { local cur prev opts ...
0
votes
0answers
16 views

How to find last x minute internet usage

How can i find out the last X minute download bandwidth in megabyte? i want to use it within a bash script.
0
votes
0answers
31 views

The program called by the init.d works by itself, but not when the init.d itself runs

I currently have two programs, my_sql_auto_update.sh and my_sql_auto_update_initd.sh, the latter is the init.d script located at ~/etc/init.d/ which [should] run the my_sql_auto_update.sh. The ...
1
vote
1answer
37 views

How to display a Image on a remote linux machine through ssh

I have two computers, one running windows(WIN7) and the other running linux(SUSe). I'm wondering if there is a way to ssh(Putty) into the linux machine and run a command to have a image display on the ...
3
votes
4answers
491 views

Is is possible to modify a file in a read only directory? [duplicate]

I have already read: Is there a way to modify a file in-place? I am curious if there is a way to modify a file in place using a command that will not create temporary files. Let's say I create a ...
2
votes
1answer
35 views

How can I popd to a specific directory?

I have used pushd to move about to various directories and now If I run dirs -v I get: 0 ~/Desktop 1 /etc 2 /var/log 3 ~/Downloads 4 /tmp How can I popd to a specific directory in the ...
1
vote
3answers
105 views

Bash long string, no word splitting?

I want to tidy up some script with long commands, so that for example: { somecheck || somecomand "reallyreallyreallyreallyreallyreallylongstring" } &> /dev/null & becomes something like: ...
0
votes
1answer
33 views

How can I get this array of numbers sorted? [closed]

I have an array called colNums that looks like this: 1 5 4 2 2 7 When I print the array with echo "$colNums" it prints like that. I want this list sorted. ...
2
votes
2answers
71 views

Why am I getting “invalid max count” from grep in an alias?

So the idea is to create an alias that will search my alias's for me. I have quite a few. dude@gnarleybox:~$ grep alg .bash_aliases.sh alias alg='alias | grep ' dude@gnarleybox:~$ alias | grep alg ...
1
vote
0answers
23 views

What Does the Output of “--help” Mean? [duplicate]

When using the --help option of a terminal program, a lot of output is produced. It usually starts with Usage: and then a bunch of things and brackets and parentheses. My question is, what doe the ...
1
vote
2answers
56 views

How do I make a BASH loop

I'm very new to bash and Linux and I was wondering how I could make a bash loop so that when it runs, it will ask you how many times to run, and it will run that many times. I did it in LUA but I ...
2
votes
3answers
88 views

bash: error handling on find -exec

I was using this little script to find malformed images: find -name '*.jpg' -exec identify -format "%f\n" {} \; 2>errors.txt It worked well enough, but now I need to modify it slightly. Instead ...
0
votes
1answer
25 views

How to include single quotes in bash folder expansion?

Edited* I have the following definitions in my bash script: DATE_SUFIX=$(date +%F_%H-%M-%S) NAME="MyBackup_" FILE_NAME=$NAME$DATE_SUFIX TMP_DIR="/tmp/" TMP_BKP=$TMP_DIR$NOME_ARQ"/" TAR_CMD="tar czPf ...