Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [for]

The tag has no usage guidance.

0
votes
1answer
31 views

List with two values in every List Item

I want to store a list with name and email address and use it in the single for loop. And I want to do it in Shell. Knowing that shell list has a white space separator list = "john, john@gmail.com" "...
0
votes
1answer
19 views

Find file while other file exist in same directory

I'm trying to do "one line script" or really small bash script. It have to find file (for example ./xxx/one.php) and if that file exist edit (with printf or echo) other file IN SAME directory (for ...
1
vote
1answer
54 views

How do I add this field?

I'm trying to print the signal strength of two wifi devices connected to a hotspot. I manage to do this script that prints the ip, hostname, mac and signal. In the script below the macs are stored in $...
-3
votes
1answer
43 views

Scripting School Assignment renaming files without extension .old to have .old [closed]

My goal is to change the file extension of files that do not have the .old extension to contain it after the script. Another goal is for me to do it with a for loop piped with grep -v to check match ...
1
vote
3answers
43 views

How can I grep each file in a directory for a keyword and output the keyword and the filename it was found in?

I have a directory with 2000+ text files. I am trying to make a script that: Reads a list of IP addresses from ip.txt Cats each file in the directory Greps each file for the IP address If keyword is ...
0
votes
3answers
48 views

Using sed command in for loop across multiple servers

I am trying to manipulate the same text file across multiple hosts. The command I currently have it: for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do ssh $host \ sudo ...
1
vote
1answer
122 views

Run Rsync for multiple files in parallel from 2 different directories and find elapsed time for each file and all files

I have lots of dump files in 2 different directories that need to be uploaded using rsync, I want to run rsync for each dump file simultaneously to save time. Dump Files: Dir1: - /u05/expdpdump/...
0
votes
1answer
29 views

Looping through a file with two columns and reading those two columns [duplicate]

I have a txt files with all the file names I need to analyze. I have this file (inputFile.txt) : /path/file1a path/file1b /path/file2a path/file2b i have a code which needs two two input files in ...
0
votes
1answer
26 views

Bash script to link networked account directories to local account directories

I'm working on a script that will create a local user account, and link local account directories to networked account directories. The only directories I would like to be created and linked are the ...
0
votes
1answer
33 views

Understanding $() (and for-loops) in bash

I have a directory with git inside and git branch gives me the following output: $ git branch branch1 * branch2 master The * marks the current branch. I would like to use this output in a script....
2
votes
2answers
62 views

Read from file then use for to split fields but honor quotes

This what I am trying to get in a bash shell script: for a in "1" "2 3" "4 5 6" do echo "a: $a" done a: 1 a: 2 3 a: 4 5 6 But the quotes are treated differently when you use a variable: echo "\"1\...
0
votes
1answer
30 views

Moving Specific Files in Specific Directory

I've some files in directory /app/me/all_off which filename like below : access_log alarm-batch alarm-service-root db_wrapper_access_log db_wrapper_api_service moqueue-job mt_push_api_service mtqueue-...
1
vote
2answers
268 views

Sum and count in for loop

I have five files each with a number in the first line. I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the ...
0
votes
2answers
20 views

How to pass arguments to a command from two files?

I'm trying to replicate the setup of 50 printers from one server to another server. Command: lpadmin -p printer_name -v printer_ip -E and some more parameters. I have the printer names in one text ...
1
vote
3answers
183 views

Bash for loop on all files in folder except one [duplicate]

I have a folder filled with 9 Robot Framework test files. Only 8 of them have to be executed, as the last one is used to set up all the others (let's call it config.robot). If I had to run all those ...
2
votes
4answers
666 views

Awk command inside a for loop

I try, with no success, to use an awk command inside a for loop. I've got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I ...
0
votes
2answers
49 views

Command substitution not working inside for loop condition

for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done Gives me an error: -bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
0
votes
1answer
83 views

Reading multiple files and operating on stored Arrays

I have 2 text files "${LinkP}" and "${QuestionP}. I want to read these files and store each complete line in the respective array, IFS=$'\r\n' GLOBIGNORE='*' command eval "LinkA=($(cat "${LinkP}"))" ...
0
votes
4answers
50 views

Looping through all sub directories in a folder?

I have requirement where in I need print relative path of each folder. Folder structure is is there any way by using single for loop I can print absolute path. output :- image_script ...
0
votes
1answer
35 views

Start range from 1 onward

I am counting multiple log files with the range starting from 1 onward however it's starting from 0. path=(/home/folder/text/*) for 1 int ${!path}; do printf "$s: $s\n" $i "${list[i]##*/}"...
0
votes
1answer
66 views

compare column of two files and print data accordingly

I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print ...
0
votes
2answers
46 views

Unix Script, problem with a 2d array

I have 2 arrays that refer to files for example alpha=file 1 beta=file2 Charlie=file3 delta=file4 beta2=file5 beta3=file6 Charlie2=file7 charlie3=file8 delta2=file9 delta3=file10 array1=("$alpha" "$...
1
vote
1answer
181 views

I want to find if a word exists in a file

I have a directory with some files. I want to know if a word exists in each file. $1 is my directory that I give it as a parameter e.g. ./myscript.sh /mnt/c/User/er/Desktop/shows I have written ...
0
votes
1answer
44 views

How to search multiple files for a specific string one by one based on partial name

I'll try to explain my problem as best as I can, but please pardon my English .. What I'm trying to do, seemed simple enough to me, however every solution I've found so far just won't work. I wanna ...
2
votes
2answers
50 views

Need to iterate through subdirectories, concatenating files, with an iterative number

I'm trying to concatenate files from three different subdirectories into one file. The names of the files in each subdirectory are exactly the same. I'd like to use a loop to iterate through the ...
-1
votes
1answer
120 views

Why does this for loop ignore my variable? [duplicate]

I am trying to do a simple script which runs a few commands N number of times, determined by the user's input. However, when it comes to run the commands (in a for loop) - The variable is ignored: ...
2
votes
4answers
353 views

Force alphabetical order in for loop with if conditions

I want to create a large number of folders and do some operations in them. The folder names are based on permutations of several chemical elements which I define as variables in a for loop: for ...
0
votes
1answer
18 views

Skip identical values in nestled for loops

I want to create directories in bash by iterating over values from multiple loops (here for simplicity just 2) while skipping identical values. A demonstrating example looks like this: for i in 1 2 3;...
0
votes
3answers
147 views

GREP for pattern and remove all the junk characters before or after the pattern

I have a set of files in a directory. And every file will have a line called ---PUBG-xxxxx-- or ---PUBG-xxxxx, PUBG-yyyyy ----. Below is the output of the grep command. grep "^--" FILE*.sql | grep "...
-1
votes
2answers
32 views

bash script looping two functions

I would like to order the 5th column by highest value and select the 4th if it matches the 5th column. cat TABLE_LIST C1 C2 C3 C4 C5 C6 3 No ...
0
votes
1answer
39 views

Why won't the for loop execute for more than 4 iterations?

In the following script, the for loop only outputs till unph4. I tried to write the value of $coils directly but doesn't work as well. I tried other iteration values to see the maximum I am getting is ...
0
votes
2answers
45 views

Excluding Bash File from modifying itself

I'm learning how to make script "fool-proof". I have some scripts that modify files in the current folder but they also modify the script itself. I know it all boils down to the "for" loop with "find" ...
2
votes
1answer
36 views

How can I sum the time based on usernames?

Basically, I want to see how much time every user spent logged in. (username) pts/0 (IP adress) Tue Dec 12 17:51 - 18:14 (00:22) - this is how one line looks in the last command. The ...
0
votes
1answer
45 views

outputting 2 positions from same file into script and then echo

I have a file which looks like this: Sender Bob IP 10.1.1.1 Sender Alex IP 10.1.1.2 Sender Jim IP 10.1.1.3 10 lines like this in same format. I need to echo Name and IP. While iterating through my ...
0
votes
3answers
152 views

unrar'ing everything in subdirectories without getting into a loop

So I've been trying to unrar multiple .rar files in lots of different folders. Some are packed a little differently and looping is causing a problem. - folder 1 - file.part01.rar - ...
1
vote
1answer
57 views

The $@ variable is not working in a for loop, trying to iterate through a users list

PROBLEM: I can't get the $@ variable to do what I want in a for loop, the loop only sends one name into the file while looping, it should loop through all the arguments and write them to the file ...
0
votes
2answers
37 views

Properly feed paths to executables in bash script

Hello all I have a text file (1.txt) that contains full paths to binaries which I need to execute a command (val_bin) for each path in the file. 1.txt Example: /source/ed/dev/ed/ab/ed_len/comercial/...
0
votes
1answer
136 views

for loop logic porting from bash to csh

I have below code snippet working in bash: for i in `ps -eaf | grep -i <pattern> | awk '{print $3}'`; do kill -9 $i; done But I have a requirement to port this code to work in csh shell due to ...
1
vote
2answers
813 views

echo list / array to xargs [duplicate]

Say I have a list/array: list=(a b c) how can I echo each element to xargs? Something like: for v in list; do echo v; done; | xargs is there a less verbose way?
0
votes
1answer
71 views

Can an array be the first argument to a function? [duplicate]

I have a function that one of the arguments is an string that has space separated words. E.g. "foo bar ccc" I think this kind of string can be treated as an "array" and loop over each word. My ...
2
votes
2answers
71 views

Default variables in Bash

The default variable for loops in Perl is $_. Is there any equivalent of this in Bash?
2
votes
1answer
137 views

Populate a CSV file from data file with nested loops in bash

I have a file, 4.txt that contains full paths to *.cfg files as well as additional data I need to strip for the final report (5.csv). For example /source/EDDG/env1/dom1/proj/config/test.cfg <...
-1
votes
1answer
308 views

sh: -c: line 1: syntax error: unexpected end of file

This command gives an error "sh: -c: line 1: syntax error: unexpected end of file" for file in /dev/DataStage/myProject/source/TEST/MyFile_*.csv; do echo "Testing" done What is wrong with the above ...
0
votes
5answers
78 views

joining each line of the file with underscore

I have an input file named test.txt like this: Homo sapiens Mus musculus Rat rattus rattus I want to use a for loop here and loop through each line. Some thing like this: for i in "`cat test.txt`" ...
0
votes
2answers
42 views

Reverse-exchange file names of files in a folder from bottom up

I have a some jpg files in a certain folder: hg_test_spr.jpg hg_test00001.jpg hg_test00002.jpg hg_test00003.jpg hg_test00004.jpg hg_test00005.jpg hg_test00006.jpg . . . hg_test01200.jpg I want to ...
0
votes
1answer
87 views

ImageMagick - Incrementing file names over several for loops

This is a Bash question about renaming / incrementing file names with a series of 'for' loops. I don't understand the behavior... Working with the ImageMagick convert command If I do this with say, ...
2
votes
1answer
39 views

for loop folder list without expansion

I try to generate a command line for a backup script on bash shell. Simple example: EXCLUDES="/home/*/.cache/* /var/cache/* /var/tmp/* /var/lib/lxcfs/cgroup/*"; for FOLDER in $EXCLUDES; do printf --...
2
votes
2answers
37 views

Ensuring that subdirectories won't be affected by a loop (or by find)

Please review these codes that are aimed to do basically the same task (finding the main .htaccess of the site and changing it): for dir in "$HOME"/public_html/*.{com,co.il}/; do if pushd "$dir"; ...
1
vote
4answers
176 views

I need to create a loop in bash to join images vertically and ouput multiple joined Pages for print

I am running Ubuntu 18.04. I have a directory full of storyboard images in .jpg format as below. image-0000.jpg image-0001.jpg image-0002.jpg image-0003.jpg image-0004.jpg . . image-3898.jpg image-...
2
votes
1answer
43 views

For loop not working in a function with arguments

So I'm creating a function that does a for loop in all the files in a directory as a given argument and prints out all the files and directories: #!/bin/bash List () { for item in $1 ...