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

learn more… | top users | synonyms (2)

2
votes
2answers
308 views

How to make this `echo | bc` output be on the same line as the echo output after?

I'm making a program for generating random numbers, and before it generates, I want to show how many combinations there are. When I do it, I get this: 64k@conroe$ bash random A-Za-z0-9 3 238328 ...
2
votes
3answers
1k views

extract string between double quotes

I have requirement that i need to extract string using quotes. My string as follows. "abcd efgh" "ijkl mnop" "qrst uvwxyz" can you help me to get the string between second double quotes(ijkl mnop) ...
7
votes
1answer
2k views

Bash substitution with variable defined from a glob pattern

The below example explains the issue. Why is the FILENAME printed correctly when echoed and perceived as a pattern when using substitution? #!/bin/bash FILEPATH_WITH_GLOB="/home/user/file_*" ...
2
votes
0answers
59 views

“: not found” and syntax error in my first ever shell script [duplicate]

The shell script, I am trying to create, should be run by crontab regularly, and it should call a perl script which saves email attachments to a predefined directory. Here's what I came up with: ...
1
vote
1answer
440 views

Problems with kpartx in a script

I am trying to run the rpi-image-creator script (see https://github.com/ImmobilienScout24/rpi-image-creator/blob/master/rpi-image-creator), and encounter some trouble with the kpartx use. The trouble ...
1
vote
1answer
138 views

Bash script to connect to Teletalk internet

I'm running Elementary OS(based on Ubuntu 12.04), and I have to connect to the internet in a peculiar style. I use a Teletalk modem to connect to the internet. Unfortunately, the modem gets recognized ...
1
vote
1answer
53 views

How can I execute and interrupt a script in a loop

I have a script called demo.py First I execute the script python demo.py -some arguments After some time (let say 120 seconds) I interrupt it with Ctrl+C and execute again. Can I write a bash ...
1
vote
1answer
118 views

Coloring a `cat` output?

I'm trying to practice code and integrating my other interests with it, and a strong interest of mine is genetics/science in general. I'm making a simple script to generate a random string, but if I ...
0
votes
2answers
46 views

filtering files from search

I have some files containing date and timestamp which I want to exclude from the output of ls as in: test.log.15-05-25_20:41:05.20150611 test.log.15-05-27_20:28:32.20150611 test.log.15-05-26_20:29:...
0
votes
4answers
106 views

tabs in variable not working

I have this lines that i will insert in a mail called "header". header=" ------------------------------------- Name\tAge\tGender\tAddress ------------------------------------- " Unfortunately, the \...
1
vote
3answers
145 views

Delete entries made in a configuration file [closed]

There are entries in a file as stated below define {line1 line 2 line 3 . . .. } define{line a line b line c . . .. } and many more are defined in similar way so, here I would like delete one ...
0
votes
1answer
84 views

Enable user login, there by login can happen either as root or user based on requirement

I am working with a board and when it finishes the boot, it gives us login as root user. What I want to do is to login as user or root based my requirement. Mean when the board boots up i should get a ...
2
votes
1answer
3k views

How to activate Virtualenv when a Python script starts?

I want to schedule a python script to run using cron on certain dates, the problem is that in order for example.py to work, example-env has to be activated, is there a way to make example.py activate ...
5
votes
5answers
2k views

How to delete files from a folder which have more than 60 files in unix?

I want to put a script in cronjob which will run in a particular time and if the file count is more than 60, it will delete oldest files from that folder. Last In First Out. I have tried, #!/bin/...
3
votes
1answer
851 views

Regex for crontab validation

I need to check in bash whether an entry into the crontab file is valid. Is there a crontab command switch? I can extract individual lines and try to control individual items through regex. But I ...
6
votes
6answers
4k views

How to keep last exit status after test

Is it possible to keep the last command exit status ($?) unaltered after a test? E.g., I would like to do: command -p sudo ... [ $? -ne 1 ] && exit $? The last exit $? should return the ...
1
vote
1answer
98 views

echo a colored text with tabs

I have the line below to echo values with tab between them. The text is also colored, however, some\t does not work. The output of this one below will have the first two \t not working. echo "\033[1;...
1
vote
2answers
99 views

shell or perl script to check group have read privileges

ls -l drwx------ 8 ws web 1024 Apr 29 2013 abc drwxr-xr-x 8 ws web 1024 Oct 19 2011 def I need some script to find directories like abc which doesn't have read/execute permissions ...
1
vote
4answers
234 views

How to insert lines from one file to another file using shell commands?

I want to write 8 lines from file1 at the beginning of file2. My file1 contains the following lines: $BQ { VOL @home } database daba relation tcdeatid copy 1 { ...
4
votes
4answers
107 views

Echoing something with multiple quotes and key characters (&, $, !, etc.)

Let's say you have to echo this into a file: RZW"a4k6[)b!^"%*X6Evf How do you do it? My actual "line" to echo is a 2048 characters line.
1
vote
4answers
379 views

Unix bash/ksh : Selection of first non space character from file from specific line

I have file file1.txt whose contents are as follows: Date List ----------- Quarter Date Year Date Month Date Now I want to read the non space elements from each row of ...
22
votes
3answers
6k views

Understand “IFS= read -r line”?

I obviously understand that one can add value to internal field separator variable. For example: $ IFS=blah $ echo "$IFS" blah $ I also understand that read -r line will save data from stdin to ...
2
votes
3answers
114 views

Truncating the precision of a date

I'm working with a bash script which polls build progress from several different systems. If a build is complete, it stats a log file to find out the last time it was edited, then feeds that time into ...
2
votes
1answer
5k views

systemd Failed at step EXEC spawning /opt/autologger/placeholder.sh: Permission denied

This is a really simple question, I know, but I am jumping right into systemd with little prior knowledge of system management and Linux in general. I am attempting to create a "Hello World" service, ...
0
votes
2answers
294 views

expect: does “” (spawn_id exp70) match glob pattern

I have written an expect script that works perfect for testing passwords on our network until it has looped through my list quite a bit, then the debug output shows that there is nothing to match. ...
4
votes
3answers
354 views

Format column-based text file into tree structure using bash

Is there a Unix/Linux command that can turn this: AMERICA USA NEW_YORK AB-100 AMERICA USA NEW_YORK VF-200 AMERICA USA NEW_YORK XY-243 AMERICA USA LOS_ANGELES UH-198 AMERICA CANADA TORONTO ...
0
votes
1answer
412 views

bash script failing to use ssh -i. Can't see directory

I am experiencing something strange. I am working on a script to remote install Splunk on servers via a script. In this script I ssh to my practice servers (5 amazon EC2 instances running Redhat). ...
0
votes
1answer
1k views

Supervisor: no such file or directory even though it is there [duplicate]

I am trying to start supervisor service supervisor start, as root, but it gives me env: /etc/init.d/supervisor: No such file or directory Yet, I can clearly see that file exists: [root@...
1
vote
2answers
1k views

while read loop

I have a file like one below var 3 2014 string var1 4 2011 string4 var2 6 1999 string2 var3 1 2016 string6 Then i have this while read loop to compare one of the ...
2
votes
2answers
647 views

How do i run a command only after previous command is unsuccessful in bash? [duplicate]

We use && operator to run a command after previous one finishes. update-system-configuration && restart-service but how do I run a command only if previous command is unsuccessful ? ...
3
votes
2answers
198 views

shell script to extract between characters

I would like to extract a user name from the file between the keywords "/" and "NET" like below. How can I do so with programs like awk, sed or cut. from: audit: command=true rsa1/abc.domain.net@...
0
votes
1answer
144 views

predetermine script input text

I have a script that automatically sets up an emailing server based off what I enter for the following echo "Please enter your main domain: (Ex.: website.com)" read DOMINIO if [ $DOMINIO = "" ]; then ...
2
votes
7answers
286 views

Restart firewall from bash file

I need to restart firewall periodically, so I created a bash file to add it to CRON tables. In terminal, I restart firewall using command: service firewall restart but if I have similar line in ...
1
vote
1answer
48 views

How to put “rm -i” in an iteration?

echo "$MOUNTPOINTLIST" | while read onelinefortouch; do touch "${onelinefortouch}testfileforwrite"; done echo "$MOUNTPOINTLIST" | while read onelineforrm; do rm -i "${onelineforrm}testfileforwrite"; ...
-1
votes
1answer
1k views

How create calculator in linux script?

I am trying to create a calculator: echo "What is your number?" read n1 echo "what is your second number?" read n2 echo "what do you want to do?" echo "1. add" echo "2. subtract" echo "3. divide" ...
1
vote
4answers
620 views

Remove all brackets in filename

I've been trying to remove all brackets in my filename(s). Can anyone help me create a bash script to do that? This bash script below work flawlessly for non-bracket filename(s), but if there are any ...
2
votes
2answers
274 views

One-liner to sort and uniq two outputs

I'm currently doing this to sort and uniq the output of two different commands: tshark -r sample.pcap -T fields -e eth.src -e ip.src > hello tshark -r sample.pcap -T fields -e eth.dst -e ip.dst &...
0
votes
1answer
52 views

How to redirect the time a task takes to complete into a log file, given that such task is already redirecting?

I have a shell script running a python task, whose stdout is being redirected to a log file: python script.py >> log.txt I wish to also log the time this task takes to complete, into the same ...
-2
votes
4answers
116 views

How to print last few contents in a line?

I have a file that has lines shown below, ta=/*92/*39/*1/*649/*12 ta=/*92/*12/*4/*342/*90 ta=/*92/*33/*9/*455/*3 ta=/*92/*12/*3/*649/*25 ta=/*92/*99/*7/*649/*7 and so on. I want a file that will ...
1
vote
2answers
1k views

How do I add ASCII art to my Bash? [duplicate]

I see other people doing this, occasionally. They'll add something like the following to the start of their terminal, sort of a welcome screen: ____ _____ _ _ _____ __ __ _ _ __ ...
0
votes
1answer
57 views

Should I move my custom commands from the end of ~/.profile to the end of ~/.zprofile when I install zsh and oh-my-zsh?

I am a new user of zsh and oh-my-zsh. Before I installed zsh and oh-my-zsh I configured my ~/.profile to start some programs automatically. For example, my ~/.profile would start guake after a 4 ...
3
votes
5answers
3k views

View a file in a tar archive without extracting it

I want to view the content of the tarred file without extracting it, Scenario: I have a.tar and inside there is a file called ./x/y.txt. I want to view the content of y.txt without actually ...
0
votes
3answers
159 views

backup script to copy files by its extension

I have created script to copy .png files from one directory to another. Script is as follows: i="`cat /usr/local/app1/default.conf | grep -i values | sed -e 's/\values=//g' -e 's/,/ /g'`" for data ...
0
votes
1answer
640 views

Create directory and files from csv file

I have a CSV file which contains the data in the following format web_url.csv drive,http://www.drive.google.com/ wikipedia,https://en.wikipedia.org/wiki/%s wolframalpha,http://www.wolframalpha.com/...
3
votes
2answers
209 views

Check if conditional with awk for matches in logfile

I want to check the auth.log file for accepted sshd connections and execute an action if any are found. Here is my code: tail -f /var/log/auth.log | awk '{ if($0 ~ /sshd/ && $0 ~ /Accepted/) ...
-1
votes
4answers
62 views

accessing loop variable in a loop [duplicate]

This is my script: load_1=15231 load_2=20547 load_3=24561 load_4=22874 f4=434 sum_1=`expr $load_1 + $f4` sum_2=`expr $load_2 + $f4` sum_3=`expr $load_3 + $f4` sum_4=`expr $load_4 + $f4` ...
2
votes
3answers
1k views

Using sed command to find the first occurrence of the pattern and append

Say there is a text file as below: Hello world types="" Mario types="Game" All I want to do is find the first occurrence of type="" and append with word program My desired output: Hello world ...
0
votes
1answer
46 views

How can I loggin from VM1 to VM2 using script?

I am using script to loggin my VM2 from VM1 (Password of the VM2: fifa): for i in {a..z}{a..z}{a..z}{a..z} do # $i as a password ssh root@ipaddress read i echo $i done bit it doesn't work. ...
5
votes
4answers
1k views

How to create loop sequence?

I am trying to create a script that will be getting one variable from user and should print a pyramid as below: * ** *** **** ***** I used this script but it shows me numbers: for i in {1..5} do ...
3
votes
2answers
149 views

Simultaneously check for empty output and successful exit status

I'd like to write the following test in an installer script1: if [ -n "`/etc/grub.d/30_os-prober`" ]; then install_dual_boot else install_linux_only fi However, it's also possible that ...