我在bash教程中遇到了这个例子:
words="foo bar doo"; for x in $words; do ...
我的理解是,在本例中,words是一个使用‘字符串和空格’定义的正则变量。作者没有解释它(这是一个初学者bash教程)。
我的问题是,bash (或可能是其他shell)的特性是什么使这一工作:
for x in $words
-I的意思是x在每次迭代时都被赋予foo、bar和doo的值?在哪里可以找到bash手册中记录的这个特性?一些链接(文档,博客)将不胜感激。
这在bash术语中怎么叫做这个结构?
words="foo bar doo
我正在根据cat/awk输出的输出执行python文件。假设我有一个类似的文件
samp.txt:
test.py test.conf
dev.py dev.conf
现在我有了一个小的shell脚本,它是cats的samp.txt文件并触发D5脚本。
samp.sh:
OLDIFS=$IFS; IFS=当我运行shell脚本时,它说python: can't open file 'test.py test.conf' : [Errno 2] No such file or directory问题在于单引号。需要一些建议来克服它。\n';for file in
我尝试使用空格以外的分隔符将一个字符串拆分成一个数组,保留空格。
例如,如果我运行:
files=$(echo "foo.txt:bar.txt:my story.txt" | tr ":" "\n")
for f in $files; do
echo $f
done
我得到了:
foo.txt
bar.txt
my
story.txt
怎样才能保留这些空间?
我有一个如下所示的变量:var regVar="Right Left Right;",然后将它们分开:regVar.split();,所以我要做的是将名为regVar的变量转化为数组。我以为有一个.array()方法,但我不知道它是如何工作的。如果有一个jQuery方法,那将是非常棒的和普通的JS。谢谢你提前帮忙!
我在标题中提到了常量索引,因为StackOverflow中所有与批处理文件中的数组索引有关的问题都集中在使用循环中的变量索引访问数组上。
我刚开始批量编写脚本。如果数组被初始化为列表(在一行中),而不是单独初始化每个元素,我希望打印具有常量索引的数组值。我编写了一个片段,其中可以打印arr的值,但不能打印list的值。
@echo off
set arr[0]=1
set arr[1]=2
set arr[2]=3
set list=1 2 3 4
REM Result is 2
echo %arr[1]%
REM Won't print
echo %list[1]%
我正在尝试创建一个bash脚本,它将读取一个有一行的文件,当它读取该行和遇到空白时,创建一个新的行,然后继续读取该行。
文件试图读取
david:69 jim:98 peter:21 sam:56 april:32
这是我当前的bash脚本
#!/bin/bash
fileName=$1 # storing the file
numberSpaces=$2 # will be used later to specify the number of spaces inbetween name:score
# Checking if no file was specified on the c
我正在尝试与某些模式匹配的grep线条,然后尝试打印那些匹配的线条。
#!/bin/bash
file=/path/to/some/file
pattern=socket
if [ -f $file ]; then
lines=`grep -i "$pattern" $file`
# Case 1
for x in $lines; do # <--- isn't this an array
echo "$x"
众所周知,路径可以在其任何组件中包含换行符。
那么,我们是否应该得出结论,环境变量$PATH可以包含换行符?
如果是这样的话,如何将$PATH分解为它的元素,类似于(Bourne ):
IFS=':' ; set -f
for var in $PATH
do
echo "<$var>"
done
但如果能做到而不改变IFS,那就更好了。
我正在尝试为类创建一个程序,当用户输入系数时,该程序计算给定方程的判别式。不过,我在将输入从字符串转换为int时遇到了问题。我的老师一心想要确保我让用户在一行中输入所有的系数,这就是问题所在。 到目前为止,我的代码如下: /*
* Write a program that will determine the number and type of roots
* for a quadratic equation, given the coefficients a, b, and c.
*/
import java.util.Scanner;
public class Main
{
我想引用access中的一个表来替换评论框中的单词。我将搜索第1列中的单词,并将其替换为第2列中的单词。我不知道如何正确命名这些列,以便将它们插入替换函数。
下面是我试图使用的代码的一个例子,
Private Sub Replace_Click()
Dim bullet As String
Dim output As String
bullet = commentBox.Value
commentBox.Value = Replace(bullet, [tbl_name].column_name, [tbl_name].column_name)
End Sub
我有一个简单的代码:
#~/bin/sh
partitions="$(ls -1 /dev/loop?* | tr '\n' ' ')"
for partition in ${partitions}
do
echo "A loop partition: ${partition}"
done
带输出
A loop partition: /dev/loop0
A loop partition: /dev/loop1
A loop partition: /dev/loop2
A loop partition: /dev/loop3
这是我的Shell脚本
#!/bin/bash
# need this or can't process file names with spaces
OIFS="$IFS"
IFS=$'\n'
file_name=file_name.xlsx
# --re-interval or the repeat character {} will not work
python ./Enforcer/scripts/xlxstocsv.py -m -s 1 -d ';' $file_name | awk --re-interval -F
我试图使用重定向(学习的目的),并想知道如何引用这个数组,以便迭代它。我知道还有其他的方法,但我试图坚持重定向。
#!/bin/bash
count=0
for i in 10.10.{0..255}.{0..255}
do
ips[$count]=$i
let count+=1
done
echo -e "$count = count\n" #test code for amount
#problem code: trying to feed the array to the while loop via redirection
while read $element;
我如何传递我的每个存储库文件并对它们做些什么?
例如,我想制作一个脚本:
#!/bin/bash
cd /myself
#for-loop that will select one by one all the files in /myself
#for each X file I will do this:
tar -cvfz X.tar.gz /myself2
我有一个bash脚本,它要求两个参数之间有一个空格。现在,我想通过读取文本文件来自动填写命令行中的提示符。文本文件包含一个带有参数组合的列表。
所以在命令行中,我想是这样的;
for line in 'cat text.file' ; do script.sh ; done
这能办到吗?我错过了什么/做错了什么?
谢谢你的帮助。
我在拆分函数时遇到了问题。我不知道它是如何工作的。
以下是我的源代码:
// Using println to print out the result
String str = " Welcome to Java Tutorial ";
str = str.trim();
String[] arr = str.split(" ");
for (String c : arr) {
System.out.println(c);
}
//Using print to print out the resu
我想倒转文本文件的文字:
如果我的意见是:
Hello World
我的产出应该是:
World Hello
我试过这个:
File.open('teste.txt').each_line do |line|
print line.reverse.gsub(/\n/,"")
end
但我把角色颠倒过来了。
我正在尝试拆分这个数组中的数据,但是它一直给我带来这样的错误:
索引超出界限。
数组的大小为10。创建错误的代码行为
int score = int.Parse(scoreInfo[1]);
这是我的代码:
static void Main(string[] args)
{
const int SIZE = 10;
for (int j = 0; j < SIZE; j++)
{
// prompt the user
Console.WriteLine("Enter player name and score on
字符串-
Bank of India,1.19E+13,06-02-2015,"BY CLG/237336/560015089/CAB,IND",140980,C,,140980,2,NA,N
我想根据逗号(,)拆分行,但不考虑字符串中的逗号。我想要数组形式的输出,如下所示:
Bank of India
1.19E+13,06-02-2015
BY CLG/237336/560015089/CAB,IND
140980
C
140980
2
NA
N
但是在string BY CLG/237336/560015089/CAB,IND中有逗号问题;以前有没有人遇到过这样的问题?
#!/usr/bin/perl
my $file = $ARGV[0];
my $value = $ARGV[1];
my @grabbed;
open (FILE, $file);
while (<FILE>) {
if (/alignment# residue#/) {
push @grabbed, $_;
while (<FILE>) {
last if /^$/;
push @grabbed, $_;
}
}
}
cl
这是我的文件,其中包含三个cloumns,并用"\t“分隔,第二个cloumn被空白分隔。
1 a b c d e Tom
2 a b Jason
3 c Lisa
4 e Kim
我想得到第二列包含两个以上元素“NF>2”的文件
1 a b c d e Tom
2 a b Jason
从埃德·莫顿的小费中获
我想创建一个小脚本,它确定一个段落中有多少个单词,然后根据一定的长度划分段落。我的方法是使用split()拆分段落,找出数组中有多少元素,然后将其中一些元素输出到一个段落中,而将其余元素输出到另一个段落中。
var para = document.getElementById('aboutParagraph').innerHTML;
var paraElements = para.split();
var paraLength = paraElements.length;
if(paraLength >= 500){
}
console.log(paraElement
在提交表单之前,我需要对一些文本域内容做一些功能,比如检查客户注册码的有效性,在客户表中将客户名称作为他的代码附加一个递增数字。
我不想在表单提交后执行此操作,因为我需要在提交表单之前在代码字段中显示它。
我的代码:
function getCode(){
var temp = document.getElementById("temp").value ;
var d = parseInt(document.getElementById("temp").value) + 1;
document.getElementById("cu